1

我有以下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)
}
4

1 回答 1

1

看来您需要保留主要对象

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

在你展示这里MyViewController()的选择器的地方没有保留,所以让它成为一个实例变量,比如

var main = MyViewController()

main.importFromFiles(origin:self)
于 2019-05-03T01:01:36.350 回答