我有以下ViewController
实现将文件导入应用程序的功能UIDocumentPickerViewController
:
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func importFromFiles(origin: UIViewController?) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeContent as String], in: .import)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = true
origin?.present(documentPicker, animated: true, completion: nil)
}
}
如您所见,该importFromFiles
方法接收一个ViewController
,它只是活动的 VC。此方法从AppDelegate
.
目前,该documentPicker
方法如下所示:
extension MyViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("Files picked")
}
}
选择器正确显示和关闭,但从不调用委托,因此print
从不执行。
编辑 1
对该importFromFiles
方法的调用发生在AppDelegate
. 更具体地说,它发生在为 SwiftTweaks 调整定义闭包时:
MyTweaks.importFromFiles.addClosure {
let topViewController = visibleViewController(root: self.window?.rootViewController)
let myVC: MyViewController = MyViewController()
myVC.importFromFiles(origin: topViewController)
}