如题,苹果自带的是没有直接dismissToRootViewController之类的方法,如果需要直接dismiss到最底层的那个控制器,可以自己写一个dismissToRootViewController方法,代码如下:
-(void)dismissToRootViewController
{
UIViewController *vc = self;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:nil];
}解释两个属性:
presentingViewController和presentedViewController
A----(present)-----B----(present)-----C
那么A就是B的presentingViewController.
C就是B的presentedViewController.
另外,self调用dismiss方法会的时候会判断self.presentedViewController是否存在,如果存在,就只会将self.presentedViewController给dismiss掉,自己不会dismiss掉。所以我们一直遍历到最底层的控制器,然后调用dismiss方法,就会将所有的presentedViewController给dismiss掉。
本文介绍了一种在iOS应用中返回到导航堆栈最底层视图控制器的方法。通过自定义dismissToRootViewController方法,开发者可以轻松地实现从当前视图控制器直接返回到最底层视图控制器的功能。文章还解释了presentingViewController和presentedViewController两个关键属性的作用。
5727

被折叠的 条评论
为什么被折叠?



