我有一个带有两个按钮的视图控制器: 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")
}
}
}