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

坑的场景是: 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轻松搞定。

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • /* UIViewController is a generic controller base class th...
    DanDanC阅读 2,069评论 0 2
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,895评论 0 10
  • 《绝爱》大结局了,每天到家吃饭的时候,可以陪着妈妈看一会连续剧,她很入剧,往往一个感人的情节让她哭的稀里哗啦,我在...
    OO碰到OO阅读 177评论 0 0
  • 1、为什么寿司之神的导演是美国人? 2、主角叫什么? 3、主角为什么要做寿司? 4、寿司之神里第一个出现的人是谁?...
    李昂_bab6阅读 590评论 0 0

友情链接更多精彩内容