函数nextTapped不会移到下一个pdf页面

时间:2019-02-19 05:46:34

标签: swift4 pdfview

函数nextTapped和函数previousTapped显示此错误“使用未解决的标识符'pdfView';您是指'PDFView'吗?”我想从函数nextTapped移至下一个pdf页面,函数previousTapped移至上一个pdf页面,当我显示pdf时我想从nextButton移到下一个pdf页面,而lastButton移到上一个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)")
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

在类中自定义pdfView,而不是在扩展名中定义它,如下所示:

//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)")
            }
        }
}

希望这可以解决您的问题。