iOS指南系列:使用QLPreviewController浏览文档

在iOS SDK包括QLPreviewControllerAPI,组件允许用户浏览许多不同的文件类型XLS文件Word文档文件PDF文件约翰创建了一个示例应用程序演示使用QLPreviewController。在示范中,您可以查看几个不同的文件类型甚至打印使用无线打印机

随着一个简短的教程,我们解释实施QLPreviewController的基础步骤你可以找到约翰的例子::::

对于过去的几个月我一直花一些时间检查出IOS快看文件预览 -接下来是一个短应用程序,我写着是为了更熟悉QLPreviewControllerAPI

对于那些熟悉读者可以这么看,quick look是一个框架,它提供快速预览一系列文件类型 -支持文件包括iWork文档微软OfficeRTF格式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 controller
	QLPreviewController *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
 

展开阅读全文

没有更多推荐了,返回首页