-1

我试图放置一个 documentPicker 以从 iCloud 获取文件,但实际上我的应用程序打开了 documentMenu 以导入文件(iCloud、Dropbox),当我选择 iCloud 时,我会在文件选择器中显示我的文件。当我必须将文件委托给 documentPicker(_ ...) [documentPicker.delegate=self.delegate] 函数时,永远不会调用函数,因为我的类不符合协议

import UIKit

class ImportKeyViewController: UIViewController{
    @IBOutlet weak var openWithLabel: UILabel!
    @IBOutlet weak var transparentBackgroundView: UIView!
    @IBOutlet weak var iCloudButton: UIButton!
    @IBOutlet weak var iTunesButton: UIButton!

    weak var delegate : UIDocumentPickerDelegate?

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){
        print("Entre a documentPicker")
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
       print("Sali a documentPicker")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("llegue")
        setUpUI()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIView.animate(withDuration: 0.1, animations: {
            self.transparentBackgroundView.backgroundColor = UIColor(red: 220/255, green: 220/255, blue: 220/255, alpha: 0.7)
        })
    }

    override func viewWillDisappear(_ animated: Bool) {
        UIView.animate(withDuration: 0.1, animations: {
            self.transparentBackgroundView.backgroundColor = UIColor.clear
        })
    }

    @IBAction func closeButtonAction(_ sender: UIButton) {

        let documentPicker = UIDocumentPickerViewController (documentTypes: ["public.text","public.content"], in: .import)
        documentPicker.delegate? = self.delegate!
        self.present(documentPicker, animated:true, completion: nil)

    }
}

我有 UIDocumentPickerDelegate 的强制性和可选方法,但它不起作用,如下图所示

这是我的第一个iOS App,希望你能帮助我。

4

1 回答 1

0

您应该添加委托方法的新语法didPickDocumentAt:,看起来它已经改变了一点。

- 编辑

ImportKeyViewController应该继承UIDocumentPickerDelegate. 一个很好的方法是创建一个扩展,这样很容易看出哪些方法是委托方法,以及你的方法是什么。

像这样:

extension ImportKeyViewController: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL){
        print("Entre a documentPicker")
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
       print("Sali a documentPicker")
    }

}
于 2017-02-23T22:06:59.210 回答