gpt4 book ai didi

ios - UIDocumentPickerViewController 未调用 documentPicker 委托(delegate)

转载 作者:行者123 更新时间:2023-11-28 05:42:52 27 4
gpt4 key购买 nike

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

}

选择器正确显示和关闭,但委托(delegate)从未被调用,因此 print 从未执行。

编辑1

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

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

最佳答案

看来您需要保留主要对象

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

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

var main = MyViewController()

main.importFromFiles(origin:self)

关于ios - UIDocumentPickerViewController 未调用 documentPicker 委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55961936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com