为PDFAnnotation设置操作时出现“无法将类型为'Void'的值分配给类型为'PDFAction?'”错误

时间:2019-01-18 15:30:47

标签: ios swift pdf pdfkit

在我的PDF Viewer应用程序中,我成功打开了PDF并显示了其所有内容。该应用将始终显示的PDF非常大,有130页。 PDF的第二页中有一个目录。我想在文本中添加PDFButton,然后滚动到用户选择的PDF中的正确页面。

我当前的代码显示一个按钮,但是我希望按钮执行的操作导致错误:

  

不能将类型为“无效”的值分配给类型为“ PDFAction”吗?

这是我尝试过的代码:

func insertResetButtonInto(_ page: PDFPage, GoTo: PDFPage) {
    let resetButtonBounds = CGRect(x: 90, y: 200, width: 200, height: 15)
    let resetButton = PDFAnnotation(bounds: resetButtonBounds, forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
    resetButton.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.button.rawValue)
    resetButton.widgetControlType = .pushButtonControl
    resetButton.backgroundColor = UIColor.green
    page.addAnnotation(resetButton)

    // Create PDFActionResetForm action to clear form fields.
    resetButton.action = pdfView.go(to: page)
}

如何在PDF中创建一个按顺序使用的按钮或将用户带到同一PDF文档中不同页面的滚动功能?

1 个答案:

答案 0 :(得分:1)

您正在尝试将函数分配给需要PDFAction的变量。将引发错误的行替换为以下内容:

resetButton.action = PDFActionGoTo(destination: PDFDestination(page: page, at: .zero))

这会将带有您先前已初始化的页面的PDFDestination传递给PDFActionGoTo(destination:)初始化程序。

如果要修改目标点,请将at初始化程序的PDFDestination参数更改为另一个CGPoint值:

let point = CGPoint(x: 50, y: 100)
resetButton.action = PDFActionGoTo(destination: PDFDestination(page: page, at: point))