iOS - 通过Quicklook打开PDF而不使用UIScrollView [英] iOS - Opening a PDF via Quicklook without using UIScrollView

查看:121
本文介绍了iOS - 通过Quicklook打开PDF而不使用UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过QuickLook框架打开PDF而不使用UIScrollView ...

I am trying to open a PDF via the QuickLook framework without using UIScrollView...

我相信我错过了一些东西......

I believe I'm missing something...

我认为我出错的地方是我需要使用QLPreviewController,而QLPreviewController是一个必须符合QLPreviewItem的dataSource。文档指出NSURL确实符合QLPriewItem所以我设置preview.dataSource到其引发错误的NSURL:

Where I believe I'm going wrong is that I need to use a QLPreviewController and on the QLPreviewController is a dataSource that has to conform to QLPreviewItem. The documentation states that NSURL does conform to QLPriewItem so I'm setting the preview.dataSource to an NSURL which is throwing an error:

<强> [NSURL numberOfPreviewItemsInPreviewController:] :无法识别的选择发送到实例

<强>终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: - [NSURL numberOfPreviewItemsInPreviewController:]:无法识别选择发送到实例0x5b5f200'

这让我觉得NSURL不符合。

Which makes me think that NSURL does not conform.

全部我认为必要的代码...

all the code I think is necessary...

- (BOOL)previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id <QLPreviewItem>)item {

    return YES;
}

- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

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

    return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

- (void)pushPDF {

    QLPreviewController *preview = [[QLPreviewController alloc] init];
    preview.dataSource = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"MCIT_Quiz" ofType:@"pdf"]];
    //preview.currentPreviewItemIndex = 0;
    [self presentModalViewController:preview animated:YES];
    [preview release];
}


推荐答案

我最后只创造了另一个class保存我的值并用作数据源,有点快速和脏,但它的工作原理。

I ended up just creating another class to hold my values and use as a datasource, a bit quick and dirty but it works.

//
//  documentList.h
//

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


@interface DocumentList : NSObject <QLPreviewControllerDataSource, QLPreviewControllerDelegate> {
    NSArray *documents;
}

@property (nonatomic, retain) NSArray *documents;

-(void)createList;
-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index;

@end

插入文本以分解文件

//
//  documentList.m
//

#import "DocumentList.h"

@implementation DocumentList

@synthesize documents;

-(void) createList {

    documents = [[NSArray arrayWithObjects:@"Quiz.pdf", nil] retain];
}

-(NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller {

    return [documents count];
}

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

return [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[documents objectAtIndex:index] ofType:nil]];
}

@end

这篇关于iOS - 通过Quicklook打开PDF而不使用UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆