iOS指南系列:使用QLPreviewController瀏覽文檔

來源:互聯網
上載者:User

在iOS SDK包括了QLPreviewControllerAPI,組件允許使用者瀏覽許多不同的檔案類型,如XLS檔案,Word文檔檔案,PDF檔案。約翰已建立了一個應用程式範例示範使用QLPreviewController。在示範中,您可以查看幾個不同的檔案類型,甚至列印(使用無線印表機。)

隨著一個簡短的教程,我們解釋實施QLPreviewController的基礎步驟,你可以找到約翰的例子::::

對於過去的幾個月中,我一直花一些時間檢查出IOS快看檔案預覽 -接下來是一個短的應用程式,我寫著是為了更熟悉QLPreviewControllerAPI。

對於那些不熟悉的讀者可以這麼看,quick look是一個架構,它提供快速預覽的一系列檔案類型 -支援的檔案包括iWork文檔,微軟Office,RTF格式,PDF格式,映像,文字檔,並以逗號分隔(CSV)檔案。

接下來在示範的程式中,我用了三個不同檔案類型,.xls/image/ms office /pdf

 

檔案預覽介面

為應用程式的介面檔案如下所示,注意QL資料來源的引用,使用的QLPreviewController時,你必須實現此協議QLPreviewControllerDataSource。這裡的唯一的執行個體變數是一個數組,包涵每個被預覽檔案的檔案字串值。UITableViewController類將用於顯示預覽的檔案清單,通過navigation到下一個預覽介面。

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

在本節中,我將展示一個選擇適用於設立預覽代碼。建立表視圖和填充相同的代碼可以被視為在Xcode項目,你可以從下面的連結下載(如果要學習tableview的使用,可以參考其它的指南)。

初始化代碼填入檔案名稱的數組: 這樣檔案名稱全在數組了:

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

下面的方法是採用QLPreviewControllerDataSource協議時,必要的兩個之一,此方法通知預覽控制器,如何在預覽導航列表中的呈現多少個項目是:

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

這種呢,算是question type的events,就是問 檔案從什麼地方來,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]];//這個代碼就體現了靈活性,你也可以寫成 ofType .pdf   return [NSURL fileURLWithPath:path];}

項目中的其餘代碼是典型的iPhone/的iOS的東西,建立應用程式委託,委託的UIWindow中添加一個子視圖(導航控制器),使視窗可見。我從下面的委託代碼,在這裡你可以得到更大的圖片視圖我如何設定此應用程式的視圖控制器。

 

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

在選中特定行的時候,初始化QLPreviewController

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

 

值得一提的是預覽控制器工作時,你有兩種不同的選擇。首先,你可以推到使用一個UINavigationController對象,你可以看到預覽控制器的對象是我做了什麼。預覽我的應用程式生命在TestViewController這個對象控制器設定為導航控制器的根視圖控制器。

第二種方法來顯示預覽控制器是模態,使用方法presentModalViewController。  //這個和我上篇fastpdfkit的講法是一致的:

#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);}

以上就是QLPreviewController的一些delegate,首先是預覽頁面數目,其次是我需要的URL(NSURL),最後是這個view的支援rotation程度。



 首先雙tap,然後拖動,出現context view,然後選擇功能,copy/define(dictionary)。

====補充:

問題:如何刪除那個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 about QLPreviewController is that it's not meant to be customized. I have built a subclass ofQLPreviewController that 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 should notice this in my code. AnytimeRBFilePreviewer removes the action button, you just need to re-add
your custom buttons. To add your custom buttons, you should create aUIBarButtonItem that holds a custom view with four buttons in it. Then set the right bar button item as the customUIBarButtonItem you created.

Update:

I've updated RBFilePreviewer to allow you to set a custom right bar button item right out-of-the-box. Just call-setRightBarButtonItem: on
RBFilePreviewer and it just works.

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

RBFilePreviewer is a subclass of QLPreviewController. 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 providedRBFile class or any other class that conforms to
QLPreviewItem
(includingNSURL).

 

hint:smples/SmoothDocumentLoaderProject
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.