将QLPreviewController添加为子视图不会加载PDF [英] Adding QLPreviewController as subview doesn't load PDF

查看:198
本文介绍了将QLPreviewController添加为子视图不会加载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将QLPreviewController的视图添加为子视图(不 - 我不能使用导航控制器或模态)。它只显示QLPreviewController的结构背景。

I'm trying to add a QLPreviewController's view as a subview (no--I cannot use a nav controller or modal). It only shows the fabric background of the QLPreviewController.

我创建一个并将其添加为子视图:

I create one and add it as a subview:

QLPreviewController* preview = [[[QLPreviewController alloc] init] autorelease];
preview.dataSource = self;
preview.delegate = self;
preview.view.frame = CGRectMake(0, 0, self.pdfPreviewView.frame.size.width, self.pdfPreviewView.frame.size.height);
self.pdfPreviewView.previewController = preview;
[self.pdfPreviewView addSubview:preview.view];
[preview reloadData];

我的QLPreviewControllerDataSource方法工作正常(一次查看1个pdf):

My QLPreviewControllerDataSource methods work fine (viewing 1 pdf at a time):

- (id <QLPreviewItem>) previewController: (QLPreviewController *) controller previewItemAtIndex: (NSInteger) index
{
    NSString *path = [[ResourceManager defaultManager] pathForPDF:self.currentPDF];
    NSURL *url = [NSURL fileURLWithPath:path];

    if ([QLPreviewController canPreviewItem:url]) {
        return url; // This always returns
    }

    return nil; // This line is never executed
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller
{
    return 1;
}

数据源方法总是返回文件url,而QLPreviewController说它可以打开该文件,但它实际上从来没有。我只是得到了背景。在创建QLPreviewController之前设置了 self.currentPDF ,并确实包含了正确的信息(来自CoreData)。

The data source method always returns the file url, and QLPreviewController says it can open the file, but it never actually does. I just get the background. The self.currentPDF is set before I create the QLPreviewController and does contain the correct information (from CoreData).

委托方法永远不会被调用。但我也没有以标准方式使用它,所以这并非完全出乎意料。

The delegate methods never get called. But I'm also not using it in a standard way, so that's not totally unexpected.

我也试过调用 [预览setNeedsLayout] [预览setNeedsDisplay'] [预览refreshCurrentPreviewItem] 但是那些只是调用数据源方法,不要改变任何东西。

I've also tried calling [preview setNeedsLayout], [preview setNeedsDisplay'], and [preview refreshCurrentPreviewItem] but those just call the data source methods and don't change anything.

PDF有效。我可以在Xcode和Preview中打开它们,所以这不是问题。我有点难过为什么这不起作用。任何帮助将不胜感激使这个工作。

The PDFs are valid. I can open them in both Xcode and Preview, so that's not the problem. I'm kind of stumped as to why this won't work. Any help would be appreciated in getting this to work.

推荐答案

原来我发送QLPreviewController错误的路径。它没有正确找到包中的PDF。我需要使用 pathForResource:ofType:inDirectory

Turns out I was sending QLPreviewController the wrong path. It wasn't finding the PDF in the bundle correctly. I needed to use pathForResource:ofType:inDirectory.

这篇关于将QLPreviewController添加为子视图不会加载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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