UIDocumentPickerViewController未调用documentPicker委托

时间:2019-05-03 00:45:08

标签: ios swift delegates uidocumentpickerviewcontroller

我具有以下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

欢迎任何提示。

编辑1

importFromFiles方法的调用在AppDelegate内部进行。更具体地说,它是在为SwiftTweaks调整定义闭包时发生的:

MyTweaks.importFromFiles.addClosure {
  let topViewController = visibleViewController(root: self.window?.rootViewController)
  let myVC: MyViewController = MyViewController()
  myVC.importFromFiles(origin: topViewController)

}

1 个答案:

答案 0 :(得分:1)

看来您需要保留主要对象

let myVC: MyViewController = MyViewController()
myVC.importFromFiles(origin: topViewController)

MyViewController()此处未显示选择器的内部,因此将其设置为像var这样的实例

var main = MyViewController()

main.importFromFiles(origin:self)