1.使用UINavigationController的pushViewController:animation方法
if (self.navigationController) {
[self.navigationController pushViewController:newViewController animated:YES];
}
返回之前的视图:
[self.navigationController popViewControllerAnimated:flag];
ps: push以后会在navigation的left bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。
2.直接使用UIViewController的presentViewController:animation:completion方法
[self presentViewController:retrieveViewController animated:YES completion:nil];
返回之前的视图:
[self.presentingViewController dismissViewControllerAnimated:flag completion:completion];
3.addSubView
UINavigationController是导航控制器,如果pushViewController的话,会跳转到下一个ViewController,点返回会回到现在这个ViewController;
如果是addSubview的话,其实还是对当前的ViewController操作,只是在当前视图上面又“盖”住了一层视图,其实原来的画面在下面呢,看不到而已
另加一个
使用presentViewControllerAnimated方法从A->B->C,若想在C中直接返回A,则可这样实现:
C中返回事件:
- void back
- {
- [self dismissViewControllerAnimated:NO];//注意一定是NO!!
- [[NSNotificationCenter defaultCenter]postNotificationName:@"backback" object:nil];
- }
然后在B中,
- //在viewdidload中:
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"backback" object:nil];
- -(void)back
- {
- [self dismissModalViewControllerAnimated:YES];
- }