0

我正在将 Xcode 9.4 与 Objective C 一起使用。我想按照以下代码使用 PDFView 显示 pdf 文件

PDFView * pdfView = [[PDFView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
NSString* path = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"pdf"];
NSData* data = [NSData dataWithContentsOfFile:path];
PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:data];
[pdfView setDocument:pdfDocument];

但它与 SetDocument 无法识别的选择器实例以及以下错误一起失败。

错误:

-[PDFView setDocument:]:无法识别的选择器发送到实例 0x101367c40

错误:执行被中断,原因:尝试取消引用无效的 ObjC 对象或向其发送无法识别的选择器。该过程已返回到表达式评估之前的状态。

我无法确定确切的问题,请帮助。

谢谢你。

4

3 回答 3

3

最后,

我得到了解决方案,只需在情节提要中添加 UIView 并将类设置为检查器中的 PDFView。并创建 PDFView 的 IBOutlet 对象并将其分配给情节提要中的 UIView 并在需要时将 pdfdocument 连同属性一起设置为 PdfView。

代码:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Swift" ofType:@"pdf"]];
    PDFDocument *document = [[PDFDocument alloc]initWithURL:url];
    _pdfView.document = document;
    _pdfView.frame = self.view.frame;
    _pdfView.displayDirection = kPDFDisplayDirectionHorizontal;

感谢大家为讨论所做的宝贵努力。

于 2018-11-20T20:47:37.237 回答
1

根据 Apple 的 PDFView 文档,有一个document保存 PDF 文档的属性。而不是调用setDocument,将document属性设置为您的pdfDocument变量。

于 2018-11-20T19:27:15.110 回答
1

我遇到了同样的问题,对我来说,原因是另一个开发人员在我们的项目中添加了 pod“UIImage-PDF”,它还提供了一个名为 PDFView 的类,但界面不同。

把这个答案放在这里,以防其他人与我有同样的根本原因。

于 2020-05-04T16:06:58.383 回答