我试图放置一个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应用程序,希望您能帮助我。
您应该添加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")
}
}