iOS文檔預覽功能教程

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   os   sp   for   檔案   

 本文轉載至 http://blog.csdn.net/devday/article/details/6580444 文檔iosuinavigationcontrollerextensionmicrosoftcomponents

ios 4 sdk中支技文檔的預覽功能,何為預覽?就是你列印檔案時的預覽功能。其用到quicklook.framework,它支援的文檔格式有: iWork documents, Microsoft Office, Rich Text Format, PDF, images, text files and comma-separated (csv) files.

 

今天show一個demo,展示其用法:

 

第一步:建立一個基於view的工程,並加入quicklook.framewrok

第二步:修改Controller的標頭檔如下:

 

  1. #import <QuickLook/QuickLook.h>  
  2.    
  3. @interface TestViewController : UITableViewController <QLPreviewControllerDataSource>  
  4. {  
  5.   NSArray *arrayOfDocuments;  
  6. }  
  7.    
  8. @end  

修改 controller執行檔案如下

 

 

  1. #import "TestViewController.h"  
  2.   
  3. @implementation TestViewController  
  4.   
  5. #pragma mark -  
  6. #pragma mark Initialization  
  7.   
  8. /*--------------------------------------------------------------------------- 
  9. *   
  10. *--------------------------------------------------------------------------*/  
  11. -(id)init  
  12. {  
  13.   if (self = [super init])  
  14.   {  
  15.         arrayOfDocuments = [[NSArray alloc] initWithObjects:   
  16.             @"iOSDevTips.png", @"Remodel.xls", @"Core J2ME Technology.pdf", nil];  
  17.   
  18.   }  
  19.   return self;  
  20. }  
  21.   
  22. /*--------------------------------------------------------------------------- 
  23. *   
  24. *--------------------------------------------------------------------------*/  
  25. - (void)loadView   
  26. {  
  27.     [super loadView];  
  28.   
  29.     [self setTitle:@"Files Available for Preview"];  
  30. }  
  31.   
  32. #pragma mark -  
  33. #pragma mark Table Management  
  34.   
  35. // Customize the number of sections in the table view.  
  36. /*--------------------------------------------------------------------------- 
  37. *   
  38. *--------------------------------------------------------------------------*/  
  39. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView   
  40. {  
  41.     return 1;  
  42. }  
  43.   
  44. /*--------------------------------------------------------------------------- 
  45. *   
  46. *--------------------------------------------------------------------------*/  
  47. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
  48. {  
  49.     return [arrayOfDocuments count];  
  50. }  
  51.   
  52. /*--------------------------------------------------------------------------- 
  53. *   
  54. *--------------------------------------------------------------------------*/  
  55. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  56. {  
  57.   static NSString *CellIdentifier = @"tableRow";  
  58.     
  59.   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  60.   if (cell == nil)  
  61.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  62.       
  63.     // ???  
  64.     [[cell textLabel] setText:[arrayOfDocuments objectAtIndex:indexPath.row]];  
  65.     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];  
  66.   
  67.     return cell;  
  68. }  
  69.   
  70. /*--------------------------------------------------------------------------- 
  71. *   
  72. *--------------------------------------------------------------------------*/  
  73. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
  74. {    
  75.     // When user taps a row, create the preview controller  
  76.     QLPreviewController *previewer = [[[QLPreviewController alloc] init] autorelease];  
  77.   
  78.     // Set data source  
  79.     [previewer setDataSource:self];  
  80.     
  81.   // Which item to preview  
  82.     [previewer setCurrentPreviewItemIndex:indexPath.row];  
  83.   
  84.     // Push new viewcontroller, previewing the document  
  85.     [[self navigationController] pushViewController:previewer animated:YES];  
  86. }  
  87.   
  88. #pragma mark -  
  89. #pragma mark Preview Controller  
  90.   
  91. /*--------------------------------------------------------------------------- 
  92. *   
  93. *--------------------------------------------------------------------------*/  
  94. - (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller   
  95. {  
  96.     return [arrayOfDocuments count];  
  97. }  
  98.   
  99. /*--------------------------------------------------------------------------- 
  100. *   
  101. *--------------------------------------------------------------------------*/  
  102. - (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index   
  103. {  
  104.     // Break the path into it‘s components (filename and extension)  
  105.     NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];  
  106.   
  107.     // Use the filename (index 0) and the extension (index 1) to get path  
  108.   NSString *path = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]];  
  109.                                
  110.     return [NSURL fileURLWithPath:path];  
  111. }  
  112.   
  113. #pragma mark -  
  114. #pragma mark Cleanup  
  115.   
  116. /*--------------------------------------------------------------------------- 
  117. *   
  118. *--------------------------------------------------------------------------*/  
  119. - (void)dealloc   
  120. {  
  121.     // Free up all the documents  
  122.     [arrayOfDocuments release];  
  123.   
  124.     [super dealloc];  
  125. }  
  126.   
  127. @end  

修改Appdelegate如下

 

 

  1. - (void)applicationDidFinishLaunching:(UIApplication *)application   
  2. {     
  3.   // Create and initialize the window  
  4.   window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  5.    
  6.   // Create test view controller  
  7.   vc = [[TestViewController alloc] init];  
  8.    
  9.   // Create navigation controller   
  10.   nav = [[UINavigationController alloc] initWithRootViewController:vc];  
  11.    
  12.   [window addSubview:[nav view]];    
  13.   [window makeKeyAndVisible];  
  14. }  

所要的資源檔可以源碼中找到。

iOS文檔預覽功能教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.