[英]Missing “Select” option to select multiple files in UIDocumentPickerViewController
[英]How to set maximum files to select in UIDocumentPickerViewController
我需要设置一些可以通过启用UIDocumentPickerViewController allowsMultipleSelection
进行选择的文件,但是我没有找到可用于设置此属性的任何属性。
有可能的?
为此,您需要实现UIDocumentPickerDelegate
的didPickDocumentsAt
。 它看起来像这样:
class YourViewController: UIDocumentPickerViewController {
let maxDocs = 3
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
extension YourViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
// check to make sure you haven't hit your document cap specified above
guard urls.count < maxDocs else { return }
// if you pass the guard, business as usual
}
}
我以前没有上过这个课,所以可能会有一些粗糙的地方,但这就是诗人如何做的物理方法。 您可能需要对它进行一些改进,因为我不确定委托方法的URL来自何处。 您可以在调用此方法时抛出一个断点,并输入po urls
以查看其中的内容。
在查看该类可用的委托方法时,我看不到用于选择单个文档的委托方法,因此您需要仔细研究一下作为委托方法中参数的URL数组会发生什么并弄清楚您需要执行多少簿记处理在selected和!selected之间进行状态切换。
您需要使用:
UIDocumentBrowserViewController
而不是UIDocumentPickerViewController。 它将允许您选择多个项目:
let document = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"])
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) {
print("result.........\(documentURLs)")
}
override func viewDidLoad() {
super.viewDidLoad()
document.delegate = self
document.allowsPickingMultipleItems = true
}
并且不要忘记将UIDocumentBrowserViewControllerDelegate添加到您的VC中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.