IOS document preview function tutorial

Source: Internet
Author: User

What is the preview function of technical documents in IOS 4 SDK? Is the preview function when you print files. It is used in quicklook. Framework and supports the following file formats: iwork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (CSV) files.


Show a demo today to show its usage:


Step 1: Create a view-based project and add quicklook. framewrok

Step 2: Modify the Controller header file as follows:

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

Modify the Controller execution file as follows:

#import "TestViewController.h"@implementation TestViewController#pragma mark -#pragma mark Initialization/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/-(id)init{  if (self = [super init])  {arrayOfDocuments = [[NSArray alloc] initWithObjects: @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];  }  return self;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)loadView {[super loadView];[self setTitle:@"Files Available for Preview"];}#pragma mark -#pragma mark Table Management// Customize the number of sections in the table view./*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return [arrayOfDocuments count];}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  static NSString *CellIdentifier = @"tableRow";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  if (cell == nil)cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    // ???[[cell textLabel] setText:[arrayOfDocuments objectAtIndex:indexPath.row]];[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];return cell;}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (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];}#pragma mark -#pragma mark Preview Controller/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {return [arrayOfDocuments count];}/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {// Break the path into it's 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]];                             return [NSURL fileURLWithPath:path];}#pragma mark -#pragma mark Cleanup/*---------------------------------------------------------------------------*  *--------------------------------------------------------------------------*/- (void)dealloc {// Free up all the documents[arrayOfDocuments release];[super dealloc];}@end

Modify appdelegate as follows:

- (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];}

You can find the resource file in the source code.


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.