美文网首页
模态视图上在套模态视图的坑!!!

模态视图上在套模态视图的坑!!!

作者: 三三哥 | 来源:发表于2018-05-10 10:57 被阅读0次

    坑的场景是: UIViewController A 推出模态视图B, B 在推出模态视图C,用B dismiss 的时候  直接退出到A,而不是把C 给退出,停留在B 控制器。

    苹果官方文档解释:

    Dismisses the view controller that was presented modally by the view controller.

    The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.

    翻译过来就是谁呈现,谁处理!这句话是没错。但是遇到上述坑的场景就爽了,上述场景我相信大家肯定要退出C 停留在B,但是实际情况是B 也被干掉了。

    举个实际例子: UIViewController A 是 UINavigationController* navA 的子视图控制器, 此时用A presenet 一个模态视图 B, B 上面在推出一个UIAlertViewController 系统提示弹框。

    此时 我以前思维就是在B 的控制器里面用  [self presentViewController:alertVC animated:YES completion:nil];

    那么在alertVC 的取消或者确定按钮的block里面遵循谁呈现,谁处理的原则: 用 [self dismissViewControllerAnimated:YES completion:nil];

    结果直接退到A而不是我想要的留在B上。

    这个时候应该用  [alertVC dismissViewControllerAnimated:YES completion:nil];才正确。系统文档也给出了:

    If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

    If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method.

    The completion handler is called after the viewDidDisappear: method is called on the presented view controller.

    系统文档告诉我们 需要用堆栈中较高的控制器去dismiss,我们的场景我理解就是用alertVC 而不是习惯性的用B 去dismiss,

    保留presentedViewController 的引用,然后在viewDidDisappear 去处理,反正我半天是没处理成功。最后用alertVC轻松搞定。

    又踏平了一个以前百用而不知的坑!算是一个进步把!

    相关文章

      网友评论

          本文标题:模态视图上在套模态视图的坑!!!

          本文链接:https://www.haomeiwen.com/subject/vrhzrftx.html