IOS Guide Series: Use qlpreviewcontroller to browse documents

Source: Internet
Author: User

The ios sdk includes the qlpreviewcontrollerapi, which allows users to browse many different file types, such as XLS files, Word document files, and PDF files. John has created a sample application to demonstrate the use of qlpreviewcontroller. In the demo, you can view several different file types or even print them (using a wireless printer .)

With a short tutorial, we will explain the basic steps for implementing qlpreviewcontroller. You can find John's example ::::

For pastMonthsIn, I 've been spending some time checking out the IOS Quick View File preview-next is a short application that I wrote to become more familiar with the qlpreviewcontrollerapi.

Quick look is a framework that provides a series of file types for quick preview. Supported files include iwork documents, Microsoft Office, RTF, and PDF, images and text files separated by commas.

In the demo program, I use three different files, .xls/image/MS Office/PDF

 

File preview page

The interface file for the application is as follows. Pay attention to the reference of the Ql data source. When using qlpreviewcontroller, you must implement this protocol.Qlpreviewcontrollerdatasource. The unique instance variable here is an array containing the string values of each file to be previewed. The uitableviewcontroller class displays the list of preview files and uses navigation to go to the next preview page.

#import <QuickLook/QuickLook.h> @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>{  NSArray *arrayOfDocuments;} @end

In this section, I will show a selection for setting up preview code. The code for creating and filling a table view can be considered as in the xcode project. You can download the code from the following link (if you want to learn how to use tableview, refer to other guides ).

Fill in the array of file names in the initialization code: in this way, the file names are all in the array:

-(id)init{  if (self = [super init])  {    arrayOfDocuments = [[NSArray alloc] initWithObjects:         @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];  }  return self;}

The following method is required when the qlpreviewcontrollerdatasource protocol is used. This method notifies the preview controller of how many items are displayed in the preview navigation list:

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {  return [arrayOfDocuments count];}

This is the events of question type, which is to ask where the file comes from, nsurl

-(ID <qlpreviewitem>) previewcontroller: (qlpreviewcontroller *) controller previewitematindex :( nsinteger) index {// break the path into its components (filename and extension) nsarray * filecomponents = [[arrayofdocuments objectatindex: Index] componentsseparatedbystring :@". "]; // use the filename (index 0) and the extension (Index 1) to get path nsstring * Path = [[nsbundle mainbundle] pathforresource: [filecomponents objectatindex: 0] oftype: [filecomponents objectatindex: 1]; // This code shows flexibility. You can also write it as oftype. PDF return [nsurl fileurlwithpath: path];}

The rest of the Code in the project is a typical iPhone/IOS thing. Create an application delegate and add a subview (navigation Controller) to the delegate uiwindow to make the window visible. From the delegate code below, you can get a larger picture view here. How can I set the View Controller of this application.

 

- (void)applicationDidFinishLaunching:(UIApplication *)application {     // Create and initialize the window  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];   // Create test view controller  vc = [[TestViewController alloc] init];   // Create navigation controller   nav = [[UINavigationController alloc] initWithRootViewController:vc];   [window addSubview:[nav view]];    [window makeKeyAndVisible];}

InitializeQlpreviewcontroller:

/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  // When user taps a row, create the preview controllerQLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];// Set data source[previewer setDataSource:self];    // Which item to preview[previewer setCurrentPreviewItemIndex:indexPath.row];// Push new viewcontroller, previewing the document[[self navigationController] pushViewController:previewer animated:YES];}

 

It is worth mentioning that you have two different options when previewing the controller. First, you can push to a uinavigationcontroller object. You can see what the preview controller object is. Preview my app life in testviewcontroller this object controller is set as the navigation controller's Root View Controller.

The second method is to display that the preview controller is a mode. Use the presentmodalviewcontroller method. // This is the same as my previous fastdevelopment kit statement:

#pragma mark -#pragma mark QLPreviewControllerDataSource// Returns the number of items that the preview controller should preview- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController{    return 5; //30//you can increase/decrease the this}// returns the item that the preview controller should preview- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx{    return fileURL;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return (interfaceOrientation == UIInterfaceOrientationPortrait);}

The above are some Delegate of qlpreviewcontroller. The first is the number of preview pages, the second is the URL (nsurl) I need, and the last is the degree of rotation supported by this view.



First, double tap, then drag, context view appears, and then select function, copy/define (dictionary ).

==== Supplement:

Question: How to delete the print button

I answered an almost identical question the other day
Here. The question pertained to removing the print button, which isn' t too hard. One thing to note aboutQLPreviewControllerIs that it's not meant to be customized. I have built a subclassQLPreviewControllerThat can be customized.
I 've put it here on GitHub. it's designed to easily remove the action button, among other features too. it wouldn't take much effort at all to replace the button with a custom one.

The biggest thing to watch out for is that the action button is re-added to the navigation bar anytime a new document is displayed. You shoshould notice this in my code. AnytimeRBFilePreviewerRemoves the action button, you just need to re-add
Your custom buttons. To add your custom buttons, you shoshould createUIBarButtonItemThat holds a custom view with four buttons in it. Then set the right bar button item as the customUIBarButtonItemYou created.

Update:

I 've updatedRBFilePreviewerTo allow you to set a custom right bar button item right out-of-the-box. Just call-setRightBarButtonItem:On
RBFilePreviewerAnd it just works.

Https://github.com/rob-brown/RBFilePreviewer

RBFilePreviewerIs a subclassQLPreviewController. It is intended to make it easy to preview anyQLPreviewItem. All you need to do is pass it the desired item (s) to preview to the appropriate initializer. You may
Use the providedRBFileClass or any other class that conforms
QLPreviewItem
(IncludingNSURL).

 

Hint: smples/smoothdocumentloaderproject
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.