在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 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