【问题标题】:iOS - Opening a PDF via Quicklook without using UIScrollViewiOS - 通过 Quicklook 打开 PDF 而不使用 UIScrollView
【发布时间】:2011-04-28 13:16:55
【问题描述】:

我正在尝试通过 QuickLook 框架打开 PDF,而不使用 UIScrollView...

我相信我错过了什么......

我认为我出错的地方是我需要使用 QLPreviewController,而 QLPreviewController 上的数据源必须符合 QLPreviewItem。文档指出 NSURL 确实符合 QLPriewItem 所以我将 preview.dataSource 设置为抛出错误的 NSURL:

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

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[NSURL numberOfPreviewItemsInPreviewController:]:无法识别的选择器已发送到实例 0x5b5f200”

这让我觉得 NSURL 不符合。

所有我认为必要的代码......

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

【问题讨论】:

    标签: iphone objective-c ios ios4 quicklook


    【解决方案1】:

    我最终只是创建了另一个类来保存我的值并用作数据源,有点快速和肮脏,但它有效。

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

    【讨论】:

    • 如果你真的只有一个,那何不把文件去掉,直接返回1,把quiz.pdf放到previewItemAtIndex中
    • 这行得通吗?文档说明它需要一个 int 而不是那里的文件名。
    • 啊,我明白你的意思了。 Quiz.pdf 是动态的,上面只是一个简单的例子。
    • @Mytheral - 我在看这个答案,因为我有同样的问题。我刚刚开始处理这个问题,你能告诉我这些方法的实际调用方式/位置吗?
    【解决方案2】:

    嗯,我看不到 NSURL 符合 QLPreviewControllerDataSource 的地方。我想你想要

     preview.dataSource = self;
    

    然后您已经编写的例程(numberOfPreviewItemsInPreviewController 和 previewController)将返回适当的 NSURL(尽管不清楚“文档”是如何填充的。)。

    【讨论】:

    • 当类本身符合时,它会起作用......但这就是我试图不做的事情。
    • 好吧,我认为我的观点仍然正确,即您的代码必须是提供预览项目的数据源,无论您是否选择将其分解到另一个类中。
    • 谁能告诉我如何自定义这个(QLPreviewController)?我想在这里为用户提供一些选项,例如文本搜索、打印以及下一个和上一个按钮。
    猜你喜欢
    • 1970-01-01
    • 2020-04-28
    • 2023-03-15
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多