多控制器跳转—present

1:present 控制器的使用


使用present的方式,从一个控制器跳转到另一个控制器的方法如下:

[self  presentViewController:vc  animated:YES  completion:nil];

从下一个页面返回上一个页面

[self.presentingViewController   dismissViewControllerAnimated:YES  completion:nil];


2:presentedViewController 与  presentingViewController


假设从A控制器通过present的方式跳转到了B控制器,那么 A.presentedViewController 就是B控制器;B.presentingViewController 就是A控制器。


3.A->B,B->C,C 如何直接 跳回 A controller?


直接在 C 界面调用

[self.presentingViewController.presentingViewController   dismissViewController   Animated:YES  completion:nil];


4.如果多个控制器都通过 present 的方式跳转呢?比如从A跳转到B,从B跳转到C,从C跳转到D,如何由D直接返回到A呢?


可以通过presentingViewController 一直找到A控制器,然后调用A控制器的

dismissViewControllerAnimated 方法。方法如下:

UIViewController *controller = self;

while(controller.presentingViewController != nil){

    controller = controller.presentingViewController;

}

[controller  dismissViewController  Animated:YEScompletion:nil];

PS:如果不是想直接返回到A控制器,比如想回到B控制器,while循环的终止条件可以通过控制器的类来判断。


关于 present 的视图层级:使用presentingViewController属性可能会遇到的坑 - 简书

官方文档:View Controller Programming Guide for iOS: Presenting a View Controller

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

相关阅读更多精彩内容

友情链接更多精彩内容