我具有以下ViewController,该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
。
欢迎任何提示。
对importFromFiles
方法的调用在AppDelegate
内部进行。更具体地说,它是在为SwiftTweaks调整定义闭包时发生的:
MyTweaks.importFromFiles.addClosure {
let topViewController = visibleViewController(root: self.window?.rootViewController)
let myVC: MyViewController = MyViewController()
myVC.importFromFiles(origin: topViewController)
}
答案 0 :(得分:1)
看来您需要保留主要对象
let myVC: MyViewController = MyViewController()
myVC.importFromFiles(origin: topViewController)
在MyViewController()
此处未显示选择器的内部,因此将其设置为像var这样的实例
var main = MyViewController()
main.importFromFiles(origin:self)