0

函数 nextTapped 和函数 previousTapped 显示此错误“使用未解析的标识符 'pdfView';你的意思是 'PDFView'?”我想从函数 nextTapped 移动到下一个 pdf 页面,并且函数 previousTapped 移动到上一个 pdf 页面,当我显示我想要的 pdf 时nextButton 移动到下一个 pdf 页面,previousButton 移动到上一个 pdf 页面

import UIKit
import AVFoundation
import MobileCoreServices
import PDFKit
class RecorderViewController: UIViewController {
@IBOutlet var nextButton:UIButton!
@IBOutlet var previousButton:UIButton!  
override func viewDidLoad() {
    super.viewDidLoad()
nextButton.isHidden = true
previousButton.isHidden = true
}
@IBAction func `import`(_ sender: Any) {
    let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
    documentPicker.delegate = self as? UIDocumentPickerDelegate
    documentPicker.allowsMultipleSelection = true
    present(documentPicker, animated: true, completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
@IBAction func nextTapped(_ sender: Any) {
    pdfView.goToNextPage(sender)}
@IBAction func previousTapped(_ sender: Any) {
    pdfView.goToPreviousPage(sender)
}
extension RecorderViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    guard let selectedFileURL = urls.first else {
        return
    }
    /////////her to display PDF File
    let pdfView = PDFView()
    pdfView.frame = CGRect(x: 0, y: 160, width: 1024, height: 1139)
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.view.addSubview(pdfView)

    pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true

    let thumbnailView = PDFThumbnailView()
    thumbnailView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(thumbnailView)

    thumbnailView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    thumbnailView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    thumbnailView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

    pdfView.bottomAnchor.constraint(equalTo: thumbnailView.topAnchor).isActive = true
    pdfView.heightAnchor.constraint(equalToConstant: 150)

    pdfView.displayMode = .singlePage
    // Fit content in PDFView.

    nextButton.isHidden = false
    previousButton.isHidden = false

    pdfView.autoScales = true
    pdfView.document = PDFDocument(url: selectedFileURL)


    let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let sandboxFileURL = dir.appendingPathComponent(selectedFileURL.lastPathComponent)

    ///i need to ensure the file is saved
   if FileManager.default.fileExists(atPath: sandboxFileURL.path) {
        print("Already exists! Do nothing")
    }
    else {

        do {
            try FileManager.default.copyItem(at: selectedFileURL, to: sandboxFileURL)

            print("Copied file!")
        }
        catch {
            print("Error: \(error)")
        }
    }
}

}
4

1 回答 1

0

在 Class 中定义您的 pdfView,而不是在 Extension 中,如下所示:

//Add PDF View Object here not in extension
    let pdfView = PDFView()

只需像这样稍微更新您的代码:

import UIKit
import AVFoundation
import MobileCoreServices
import PDFKit

class RecorderViewController: UIViewController {

    //Add PDF View Object here not in extension
    let pdfView = PDFView()

    @IBOutlet var nextButton:UIButton!
    @IBOutlet var previousButton:UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        nextButton.isHidden = true
        previousButton.isHidden = true
    }

    @IBAction func `import`(_ sender: Any) {
        let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String], in: .import)
        documentPicker.delegate = self as? UIDocumentPickerDelegate
        documentPicker.allowsMultipleSelection = true
        present(documentPicker, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func nextTapped(_ sender: Any) {
        pdfView.goToNextPage(sender)
    }

    @IBAction func previousTapped(_ sender: Any) {
        pdfView.goToPreviousPage(sender)
    }

}

extension RecorderViewController: UIDocumentPickerDelegate {
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let selectedFileURL = urls.first else {
            return
        }
        /////////her to display PDF File

        pdfView.frame = CGRect(x: 0, y: 160, width: self.view.frame.size.width, height: self.view.frame.size.height - 160)
        pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.view.addSubview(pdfView)

        pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true

        let thumbnailView = PDFThumbnailView()
        thumbnailView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(thumbnailView)

        thumbnailView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        thumbnailView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        thumbnailView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

        pdfView.bottomAnchor.constraint(equalTo: thumbnailView.topAnchor).isActive = true
        pdfView.heightAnchor.constraint(equalToConstant: 150)

        pdfView.displayMode = .singlePage
        // Fit content in PDFView.

        nextButton.isHidden = false
        previousButton.isHidden = false

        pdfView.autoScales = true
        pdfView.document = PDFDocument(url: selectedFileURL)


        let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let sandboxFileURL = dir.appendingPathComponent(selectedFileURL.lastPathComponent)

        ///i need to ensure the file is saved
        if FileManager.default.fileExists(atPath: sandboxFileURL.path) {
            print("Already exists! Do nothing")
        }
        else {

            do {
                try FileManager.default.copyItem(at: selectedFileURL, to: sandboxFileURL)

                print("Copied file!")
            }
            catch {
                print("Error: \(error)")
            }
        }
}

希望这能解决您的问题。

于 2019-02-21T06:57:14.727 回答