在iOS界面跳转中,有两种方法:
第一种是 presentViewController,返回方式为[self dissmissModelViewControllerAnimated:YES];
第二种是用self.navigationController pushViewController,返回方式为[self.navigationController popViewControllerAnimated:YES];
延伸用法:
1.如果用present的方法从a界面到b界面再到c界面,而且再从c界面返回到a界面
需要在c界面中写一个返回方法
- (void)back {
- [self dismissModalViewControllerAnimated:NO];
-
[[NSNotificationCenter defaultCenter]postNotificationName:@"comeBack" object:nil]; //发一个通知
}
再在b界面中写以下代码
在viewDidLoad中写一个通知接收b界面发出的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"comeBack" object:nil];
然后在b界面中实现back方法- (void)back[self dismissModalViewControllerAnimated:YES];}
另:也可以自定义一个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掉。