问题描述直接借鉴(https://segmentfault.com/q/1010000004528658)segmentfault中的一个描述(
情况大致就是一个ViewController A present ViewController C,
或者根据提问者的描述 ViewController A present ViewController B 后ViewController B 再push到ViewController C
ViewController C中有一个WebView,加载了H5页面, H5页面有个按钮调用手机的相册 UIImagePickerController,弹出了拍照,相册选择的UIActionSheet, 然后点击相册按钮,程序直接退出到了 A 页面。
2.提问者尝试用A push 到B 页面,B页面push 到C 页面,这样子的话,在点击相册后会到系统相册里面,选择图片后会返回到H5页面里。
3.提问者需要的是present 也能实现而不是用push。所以尝试了以下方法
结论就是只要有present ,就是出现问题,而且提示问题就出现在present 那里
为什么这样?
虽然我们看不到点击h5弹出的UIActionSheet(暂且称作UIActionSheet),点击方法的实现。但是我们从UI交互上来看是这样的流程。
点击UIActionSheet中item => UIActionSheet dismiss => 弹出相机或者ImagePicker => 拍照或者选择图片后弹出相机或者ImagePicker dismiss
科普
presentingViewController :The view controller that presented this view controller (or its farthest ancestor. 当前ViewController present出来的ViewController
presentedViewController:The view controller that was presented by this view controller or its nearest ancestor. present当前ViewController的ViewController
分析
我们知道UIActionSheet可能是被当前ViewController C执行presentViewController模态出来的,验证猜想很简单,在ViewController 重写如下方法即可。
断点后发现弹出的viewControllerToPresent并不是UIActionSheet,而是我们很少使用的UIDocumentMenuViewController,然后并没有什么卵用,尝试hook UIDocumentMenuViewController点击方法失败。
我们知道当一个presentedViewController要dismiss的时候会调用presentingViewController的如下方法
断点后发现,dismissViewControllerAnimated执行了两次,我们的bug现象就是,dimiss掉了所有的modal试图,那么好了,问题找到了,UIDocumentMenuViewController关闭后不仅调用了自己的,dismissViewControllerAnimated,还调用了,上层或者上上层presentingViewController的dismissViewControllerAnimated。
所以使dismissViewControllerAnimated调用一次,或者让UIDocumentMenuViewController找不到presentingViewController即可。
如何解决?
方法一,使dismissViewControllerAnimated调用一次
当前ViewController的所有presentedViewController都正常执行dismissViewControllerAnimated,当前ViewController本身执行dismissViewControllerAnimated,不进行dismiss,不做处理。
当想dismiss掉当前ViewController的时候,不能调用
而是使用
即可,这样当前ViewController不会调用重写的dismissViewControllerAnimated方法,而且还顺利的dismiss掉了。
方法二,使UIDocumentMenuViewController找不到presentingViewController
断点后发调用
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag
弹出UIDocumentMenuViewController之后,又调用了
- (UIViewController *)presentingViewController
返回的presentingViewController是ViewController A,
测试发现调用ViewController A的dismissViewControllerAnimated之后,所有model出来的ViewController 都会dismiss。这也是苹果的一个特性。当模态出N个ViewController之后,只需要dismiss任意一个,都会dismiss它之后的所有模态试图
解决方法就是不让UIDocumentMenuViewController找到上层或者上上层的任意presentingViewController
Done!