【问题标题】:Opening multiple documents in a UIDocumentBrowserViewController based app在基于 UIDocumentBrowserViewController 的应用程序中打开多个文档
【发布时间】:2018-11-29 22:42:18
【问题描述】:
Apple 的文档很少涉及基于 UIDocumentBrowserViewController 的应用程序,这些应用程序希望支持同时打开多个文档。
我想启用此功能,以便用户可以轻松地在两个或多个文档之间复制/粘贴,而无需退出到文档浏览器,这在 iOS 上不是流畅的体验。
除了对allowsPickingMultipleItems 属性的简要描述外,我什么也找不到。
对于单个文档视图,Apple 建议使用模态视图,但没有说其他任何内容。
问题
- 实现多个打开文档的体验和 UI 的推荐方法是什么(如果有)?
- 有没有办法让用户打开一组文档,然后打开另一个文档,同时保持现有文档处于打开状态?
- 是否有应用可以实现这种体验?
【问题讨论】:
标签:
ios
mdi
document-based
【解决方案1】:
我是一个相对较新的 iOS 开发人员,所以对这一切持保留态度。
以下内容对我有用:
- 将allowPickingMultipleItems 设置为true
- 创建一个可以接受
URL 输入的ViewController,以及另一个可以接受[URL] 输入的ViewController。然后,这些 ViewController 必须在屏幕上显示与 URL 关联的文档。
- 可以处理一个或多个文档的单个 ViewController 也可以工作。
- 在
documentBrowser(_:, didPickDocumentURLs:) 中,检查传入了多少URL,并呈现上述ViewController 之一(视情况而定)
示例:
class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
allowsDocumentCreation = false
allowsPickingMultipleItems = true
// -snip-
}
// MARK: UIDocumentBrowserViewControllerDelegate
// -snip-
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
if documentURLs.count < 1 {
return
} else if documentURLs.count == 1 {
presentDocument(at: documentURLs[0])
} else {
presentDocuments(at: documentURLs)
}
}
// -snip-
// MARK: Document Presentation
func presentDocument(at documentURL: URL) {
// present one document
// example:
// let vc = SingleDocumentViewController()
// vc.documentURL = documentURL
// present(vc, animated: true, completion: nil)
}
func presentDocuments(at documentURLs: [URL] {
// present multiple documents
// example:
// let vc = MultipleDocumentViewController()
// vc.documentURLs = documentURLs
// present(vc, animated: true, completion: nil)
}
}
回答您的其他问题:
- 我不确定建议如何实现此功能
- 我认为打开一个,然后另一个文档可能更适合UIDocumentPickerViewController
- 我不知道有任何应用程序实现了这种多文档体验。但是,我确实知道,通过反复试验,文档浏览器看起来就像通常一样,但右上角有一个“选择”按钮。按下后,用户可以选择要打开的文档和文件夹,或“全选”。
一些注意事项:
- 如果选择了一个文件夹,并且该文件夹不在应用程序自己的目录中,则应用程序将无法访问该文件夹内的文档。
注意:
documentBrowser(_:, didPickDocumentURLs:) 将更名为 documentBrowser(_: didPickDocumentsAt:) in iOS 12