DocumentPickerDelegate无法工作后更新到SWIFT 3

问题描述:

我试图把一个documentPicker从iCloud获取文件,但实际上我的应用程序打开documentMenu导入文件(iCloud,Dropbox),当我选择iCloud时,我提出了文件选择器与我的文件。当我委派文件documentPicker(_ ...)[documentPicker.delegate = self.delegate]功能不会被调用,因为我的课不符合协议DocumentPickerDelegate无法工作后更新到SWIFT 3

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应用程序,希望你能帮助我。

+0

没有 “你可以在图像的下方看到” 有。 – Gruntcakes

+0

请通过复制和粘贴相关代码来对您的问题进行编辑,并清楚说明哪些部分有效,哪些部分没有。 – rmaddy

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

} 
+0

这一个? func documentPicker(_ controller:UIDocumentPickerViewController,didPickDocumentAt url:URL){...} –

+0

在您发布的图片中,现在已经移除了,您可能会注意到编译器错误。关于您的视图控制器没有向委托人确认。您可以通过点击命令并单击UIDocumentPickerDelegate来检出委托方法。 –

+0

现在我明白你做错了什么,让我编辑我的答案。 –