0

Apple 的文档很少涉及UIDocumentBrowserViewController基于应用程序的主题,这些应用程序希望支持同时打开多个文档。

我想启用此功能,以便用户可以轻松地在两个或多个文档之间复制/粘贴,而无需退出回文档浏览器,这在 iOS 上不是流畅的体验。

除了对该allowsPickingMultipleItems物业的简短描述外,我找不到任何东西。

对于单个文档视图,Apple 建议使用模态视图,但没有说其他任何内容。

问题

  1. 实现多个打开文档的体验和 UI 的推荐方法是什么(如果有)?
  2. 有没有办法让用户打开一组文档,然后在保持现有文档打开的同时打开另一个文档?
  3. 有没有实现这种体验的应用程序?
4

1 回答 1

2

我是一个相对较新的 iOS 开发人员,所以对这一切持保留态度。

以下对我有用:

  1. 将 allowPickingMultipleItems 设置为 true
  2. 创建一个可以接受输入的 ViewControllerURL和另一个可以接受输入的 ViewController [URL]。然后,这些 ViewController 必须在屏幕上显示与 URL 关联的文档。
    • 可以处理一个或多个文档的单个 ViewController 也可以工作。
  3. in documentBrowser(_:, didPickDocumentURLs:),检查传入了多少URLs,并呈现上述 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)
}
}

要回答您的其他问题:

  1. 我不确定如何建议实现此功能
  2. 我认为打开一个,然后另一个文档可能更适合UIDocumentPickerViewController
  3. 我不知道有任何应用程序实现了这种多文档体验。但是,我确实知道,通过反复试验,文档浏览器看起来就像通常一样,但右上角有一个“选择”按钮。按下此按钮后,用户可以选择要打开的文档和文件夹,或“全选”。

一些警告:

  • 如果选择了一个文件夹,并且该文件夹不在应用程序自己的目录中,则应用程序将无法访问该文件夹内的文档。

注意: documentBrowser(_:, didPickDocumentURLs:)documentBrowser(_: didPickDocumentsAt:) 在 iOS 12中重命名

于 2018-08-27T00:07:03.403 回答