ios - UIDocumentPickerViewController - 未调用委托(delegate)方法

标签 ios swift delegates uidocumentpickerviewcontroller

我正在使用 UIDocumentPickerViewController 从文件中选择文档并将其上传到服务器。我能够成功访问文件,但在单击文件后不会调用委托(delegate)方法。

我使用了以下代码来调用文档选择器:

class Uploads: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func uploadDocument(_ sender: Any) {

        let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText)], in: .import)
        documentPicker.delegate = self
        if #available(iOS 11.0, *) {
            documentPicker.allowsMultipleSelection = false
        } else {
        }
        present(documentPicker, animated: true, completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension Uploads: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

        print(urls.first)
    }

    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("Cancelled")
    }
}

我注意到在调用委托(delegate)方法时收到以下警告:

Instance method 'documentPicker(:didPickDocumentsAt:)' nearly matches optional requirement 'documentPicker(:didPickDocumentsAt:)' of protocol 'UIDocumentPickerDelegate'

Make 'documentPicker(_:didPickDocumentsAt:)' private to silence this warning

我相信由于这个警告,委托(delegate)方法没有被调用,尽管我不明白为什么我会收到这个警告。

最佳答案

共享代码示例,希望对您有所帮助:

class ViewController : UIViewController,UIDocumentPickerDelegate{

  var documentBrowser: UIDocumentPickerViewController = {
  let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
  let browser = UIDocumentPickerViewController(documentTypes: [documentsPath], in: .open)
      browser.allowsMultipleSelection = true
      return browser
    }()

override func viewDidLoad() {
        super.viewDidLoad()
        self.addChild(documentBrowser)
        documentBrowser.view.frame = self.view.bounds
        view.addSubview(documentBrowser.view)
 }

  func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
       print(urls)
   }
}

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

相关文章:

swift - 以编程方式调用 View 后选项卡栏消失

ios - 如何在一个类中实现两个相同类型的委托(delegate)?

swift - View Controller 内的简单数据传输,无需转换

iphone - CIHueAdjust核心图像滤镜设置

ios - 无法在 firebase 中将类型 'NSNull' 的值转换为 'NSString'

ios - Metal makeComputeCommandEncoder 断言失败

iphone - 使用返回值管理 CoreGraphics/CoreText 中的内存

ios - 替换 UICollectionView 中的 subview ,而不是更改 subview 属性

swift - 在 UIToolBar 中实现 "done"按钮来关闭 UIDatePicker 和 ToolBar

c# - 了解委托(delegate)和任务