UIDocumentPickerViewController 关闭父视图控制器 [英] UIDocumentPickerViewController dismisses parent view controller
问题描述
我在视图控制器中有一个 WKWebView
.当用户点击上传文件"按钮(在显示的网页上)时,UIDocumentPickerViewController
会弹出.这是意料之中且完全必要的,但是:
I have a WKWebView
in a view controller. When an user clicks on "Upload File" button (which is on the webpage shown), UIDocumentPickerViewController
pops up. This is expected and totally neccessary but:
每当用户单击任何按钮(上传照片或视频"、取消")时,UIDocumentPickerViewController
都会关闭自身及其所在的父视图控制器.
Whenever the user clicks on any button ("Upload Photo or Video", "Cancel"), the UIDocumentPickerViewController
dismisses itself AND the parent view controller that it's in.
我为 [UIViewController deniedViewControllerAnimated:completion:]
添加了一个符号断点,并且确实看到 -dismissViewController...
被调用了两次.在第一次关闭 UIDocumentPickerViewController
之后,在第二次之后 - 我的父视图控制器.
I have added a symbolic breakpoint for [UIViewController dismissViewControllerAnimated:completion:]
and indeed saw that -dismissViewController...
is called twice. After the first time it dismisses UIDocumentPickerViewController
, after the second one – my parent view controller.
顺便说一句,在 iPad 上没有问题,可能是因为 UIDocumentPickerViewController
以弹出框的形式呈现.
By the way, on the iPad there is no problem, probably because UIDocumentPickerViewController
is presented as a popover.
为什么会发生这种情况,我该怎么办?
Why is this happening and what should I do?
谢谢!
推荐答案
我遇到了同样的问题,终于找到了解决方法.
I faced the same problem and finally managed to find a workaround.
在我的视图控制器中,如果没有点击保存按钮,我会覆盖 dismiss
方法以删除托管对象,并且与 UIImagePickerController
完美配合.
In my view controller I override dismiss
method to delete managed object if save button wasn't hit and that worked perfectly with UIImagePickerController
.
由于我的应用程序使用 UIDocumentPickerViewController
,每次调用 documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
时,我的托管对象都会被删除,因为这会导致presentingViewController.dismiss
.
As of my application uses UIDocumentPickerViewController
, my managed object was deleted each time the documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
was called because this causes a presentingViewController.dismiss
.
所以我的解决方案是从 presentingViewController
检查 presentedViewController
是否为 nil
或者不知道是否 dismiss
方法是否被 UIDocumentPickerViewController
调用.
So my solution is to check from the presentingViewController
if the presentedViewController
is nil
or not to know if dismiss
method was called by UIDocumentPickerViewController
or not.
这里是我的视图控制器中重写的 dismiss
方法.
So here is my overriden dismiss
method from my view controller.
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
if self.presentedViewController == nil {
// dismissed by the user
myDocument.delete()
} else {
// dismissed by the UIDocumentPickerViewController
// do nothing
}
super.dismiss(animated: flag, completion: completion)
}
希望对你有帮助.
这篇关于UIDocumentPickerViewController 关闭父视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!