簡體   English   中英

iOS 未調用 UIDocumentPickerDelegate 方法(didPickDocumentsAt)

[英]UIDocumentPickerDelegate method(didPickDocumentsAt) not called by iOS

我有一個關於沒有為 DocumentPickerViewController 調用委托方法的查詢,這是背景,我只需要從我的文件應用程序中導入任何可用的資源,因此我正在使用 UIDocumentPickerViewController。

我有一個單獨的 ViewController,我將 documentPickerViewController 的視圖添加為子視圖並將其添加為委托。 我的 ViewController 的代碼是這樣的。

var documentPickerController: UIDocumentPickerViewController!
  let supportedUTI = [kUTTypeImage,kUTTypeSpreadsheet,kUTTypePresentation,kUTTypeDatabase,kUTTypeFolder,kUTTypeZipArchive,kUTTypeVideo, kUTTypeAudiovisualContent]

documentPickerController = UIDocumentPickerViewController.init(documentTypes: supportedUTI as [String], in: .import)
    documentPickerController.delegate = self
    documentPickerController.allowsMultipleSelection = false
    view.addSubview(documentPickerController.view)

現在我看到pickercontroller已打開,當我點擊取消時documentPickerWasCancelled被調用,但是當我選擇文件時documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]未被調用。

如果我像這樣直接顯示pickerViewController,我試圖進一步深入了解我所看到的是而不是顯示我的ViewController

UIDocumentPickerViewController *dc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[self UTITypes] inMode:UIDocumentPickerModeImport];
    dc.delegate = self;
    [MainVC presentViewController:dc animated:YES completion:nil];

兩個委托方法都被調用得很好。 我不明白為什么。 有人可以在這里幫我嗎! 提前致謝!!

所以我遇到了完全相同的問題,調用了documentPickerWasCancelled委托方法,但不會調用didPickDocumentsAt

另外值得注意的是,當我將所有委托/演示邏輯移動到我的基本視圖控制器中時,UIPickerDelegate 方法按預期工作。 這讓我知道沒有任何配置類型問題阻止功能。

我不確定問題出在哪里,但似乎如果文檔選擇器出現在復雜的視圖層次結構上,就會出現問題。

我最終解決此問題的方法是創建一個新窗口並在那里顯示文檔選擇器:

func showDocumentPicker() {

        let documentTypes = ["public.image", "com.adobe.pdf"]

        let picker = UIDocumentPickerViewController(documentTypes: documentTypes, in: .import)
        picker.delegate = self
        picker.allowsMultipleSelection = true
        picker.modalPresentationStyle = .formSheet

        let window = UIWindow(frame: UIScreen.main.bounds)
        let newWindowBaseVC = UIViewController()
        newWindowBaseVC.view.backgroundColor = UIColor.clear
        window.rootViewController = newWindowBaseVC
        window.windowLevel = UIWindow.Level.alert
        window.makeKeyAndVisible()
        newWindowBaseVC.present(picker, animated: false, completion: nil)
    }

答案很簡單:這是從 UIViewController 繼承的。 如果您只是將 viewController 的視圖添加到您的視圖中,則不會調用委托方法。 ViewController 有自己的生命周期。 請在此處閱讀: https ://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller

所以,為某種錯誤道歉。 當然,您可以添加一個僅顯示其視圖的子視圖控制器。 但是:我認為這不應該是用例。 它是一個全屏 ViewController 符合蘋果本身的設計指南。 話雖這么說,您應該向它展示:

func addPicker() {
    var documentPickerController: UIDocumentPickerViewController!

    documentPickerController = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF)], in: .import)
    documentPickerController.delegate = self
    documentPickerController.allowsMultipleSelection = false

    present(documentPickerController, animated: true, completion: nil)
}

有一些錯誤提交,開發人員發現在調用委托之前視圖被關閉。 據我所知,這種行為是在 ios11 中引入的,並且在顯示 viewController 時也會發生。 我真的不能說這是否是固定的,也不能說這種行為是否與將其顯示為子視圖有關。 (我認為它在某種程度上是固定的,因為它與呈現的 viewController 一起工作)

無論如何,您應該按照上面的說明呈現它,您就可以開始了。

原因是如果您不將委托放在函數外部的 var 中,則委托將被釋放。

如果您在靜態環境中,則可以創建靜態 var 委托:例如 DocumentPickerDelegateClass; 否則,在 UIViewController 中只需創建 var 委托: DocumentPickerDelegateClass

在任何情況下,都將 var 放在無法釋放的位置。 選擇“靜態”選項時要小心,必須小心使用。

在使用 iOS 15 時,我遇到了同樣的問題,你猜怎么着:雖然沒有調用 didPickdocumentsAtURL,但在這種情況調用了已棄用的 didPickDocumentAtURL。 我現在將使用它,因為無論如何我只需要單選。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM