---
title: "QLPreviewController用法"
author: "wanggp"
date: "2011-07-20T17:33:05Z"
tags: []
categories: ["移动开发"]
slug: "qlpreviewcontroller"
draft: false
lastmod: "2011-07-22T19:04:01Z"
id: 1129059
url: "https://wanggp.iteye.com/blog/1129059"
---

从IOS4.0后，apple推出新的文件预览控件：QLPreveiewController,已经支持pdf文件阅读。

用法：

1.实现QLPreviewControllerDataSource

 

```
#import <Foundation/Foundation.h>
#import <QuickLook/QuickLook.h>


@interface PreviewDataSource : NSObject<QLPreviewControllerDataSource> {
    NSString *path; 
} 

@property (nonatomic, assign) NSString *path; 

@end
#import "PreviewDataSource.h"


@implementation PreviewDataSource

@synthesize path; 


- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller 
{
	return 1;
}


- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index 
{

	return [NSURL fileURLWithPath:path];
}



- (void)dealloc { 
    [path release]; 
    [super dealloc]; 
} 
@end
```

 2.调用方法：

 

```
 QLPreviewController *previewoCntroller = [[[QLPreviewController alloc] init] autorelease];
                
                    PreviewDataSource *dataSource = [[[PreviewDataSource alloc]init] autorelease];
                    dataSource.path=[[NSString alloc] initWithString:appFile];
                    previewoCntroller.dataSource=dataSource;
                    [app.nav pushViewController: previewoCntroller animated:YES];
                    [previewoCntroller setTitle:fileName];
                    previewoCntroller.navigationItem.rightBarButtonItem=nil;
```