0

我有一个带有两个按钮的视图控制器: Button1 调出 documentPickerViewController 并让用户选择一个文件。Button2 转到第二个视图控制器。

Button1 在下面的代码中链接到“openFile”。Button2 将 segue 调用到第二个视图控制器。

所以这就是我遇到问题的方式:单击 Button1,显示文档选择器。如果我选择一个文档,那么文档选择器就会消失,我会回到视图控制器。到目前为止,一切都很好。

现在我按下Button2。第二个视图控制器出现。都好。我退出并返回到第一个视图控制器。

现在我再次按下 Button1。文档选择器出现。但这次我点击“取消”。文档选择器消失了,但第二个视图控制器出现了!

我收到“对 <_UIWaitingForRemoteViewContainerViewController: 0x122206480> 的开始/结束外观转换的不平衡调用”。和“[DocumentManager] 视图服务确实因错误而终止:Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}"

通过研究,我了解到我必须将一个额外的第二个视图控制器弹出到堆栈中,但我看不到我会在哪里完成它,以及在哪里弹出它的合适位置?

我试过设置“动画:假”,但这没有任何区别。

提前致谢。

@IBAction func openFile(_ sender: Any) {

    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.text"], in: UIDocumentPickerMode.import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
    self.present(documentPicker, animated: true, completion: nil)

}


func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    self.dismiss(animated: true, completion: nil)

}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    if controller.documentPickerMode == UIDocumentPickerMode.import {
        var textRead = ""
        do {
            if urls.count > 0 {
                for i in 0...urls.count-1 {
                    textRead = try String(contentsOf: urls[i], encoding: .utf8)
                    textView.text = textView.text + textRead
                }
            }
        }
        catch {
            /* error handling here */
            print("There's a problem reading the file")
        }

    }

}
4

2 回答 2

0

我不确定你想在你的实现中做什么,documentPickerWasCancelled(_:)但知道如果你只想隐藏文档选择器,你不需要明确调用self.dismiss(animated: true, completion: nil):当你点击“取消”时,文档选择器已经自行关闭。

于 2018-02-21T02:06:35.563 回答
0

经过更多的实验和研究,我发现这阻止了第二个视图控制器的出现,即使仍然出现两条错误消息:

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    //self.dismiss(animated: true, completion: nil)
    self.navigationController?.popToRootViewController(animated: true)
}

如果有人可以提出一个更优雅的解决方案来完全消除错误消息,我将不胜感激。

编辑:

玩了一会儿之后,我找到了解决方法!

我们只需要这个:

func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

    // do nothing. The picker is dismissed anyway.
}

因此,解雇声明导致了问题。通过忽略任何关闭选择器调用,选择器窗口无论如何都会关闭。

于 2018-02-21T01:21:39.323 回答