首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDocumentPickerViewController -代理方法未被调用

UIDocumentPickerViewController -代理方法未被调用
EN

Stack Overflow用户
提问于 2018-08-15 06:56:57
回答 2查看 2.3K关注 0票数 6

我使用UIDocumentPickerViewController从文件中选择文档并将其上传到服务器。我能够成功地访问文件,但是单击文件时委托方法不会被调用。

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

代码语言:javascript
复制
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")
    }
}

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

实例方法'documentPicker(:didPickDocumentsAt:)‘几乎匹配协议'UIDocumentPickerDelegate’的可选要求‘documentPicker(:UIDocumentPickerDelegate:)’ 使'documentPicker(_:didPickDocumentsAt:)‘私有以沉默此警告

我相信委托方法不会因为这个警告而被调用,虽然我不知道为什么要得到这个警告。

EN

回答 2

Stack Overflow用户

发布于 2019-10-30 21:43:10

希望共享代码示例将有助于:

代码语言:javascript
复制
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)
   }
}
票数 1
EN

Stack Overflow用户

发布于 2019-03-13 12:20:45

如果采用'UIDocumentPickerDelegate‘协议的类被声明为'open’,则会出现问题。

例如,这个类有一个问题:

代码语言:javascript
复制
open class FilePickerHelper: UIDocumentPickerDelegate

虽然这个类将,而不是,但它有一个问题:

代码语言:javascript
复制
class FilePickerHelper: UIDocumentPickerDelegate
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51854221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档