简介
- push与present都可以推出新的界面。
- present与dismiss对应,push和pop对应。
- present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到根
ViewController
,或其他ViewController
。 - present一般用于不同业务界面的切换,push一般用于同一业务不同界面之间的切换
push
pop一共分为两类, pop是navigationController的方法。
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController popToViewController:FirstViewController animated:YES];
平时我们可能会有这样的需求,在第一个tabBar1
的界面中,我们点击了当前页面上的某个控件,让显示第n个tabBar
上的内容,相当于从一个tabBar1
跳转另一个tabBar(n)
,其实这个很简单,在当前tabBar1
界面控件的事件中加一行代码即可
self.tabBarController.selectedIndex = 2;
样例
UIButton* pushButton = [UIButton buttonWithType:UIButtonTypeSystem];[pushButton setTitle:@"push下一个界面" forState:UIControlStateNormal];pushButton.frame = CGRectMake(100, 150, 150 , 70);[pushButton addTarget:self action:@selector(pressPushButton) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:pushButton];- (void)pressPushButton {SecondViewController* secondViewController = [[SecondViewController alloc] init];[self.navigationController pushViewController:secondViewController animated:YES];
}
present
- present多个视图控制器的时候,系统维护了一个栈,栈底到栈顶依次是A->B->C->D,当在A中执行
dismiss
方法,栈中在A之上的视图都会被dismiss。不同的是,栈顶的视图控制器将会以动画方式被dismiss,而中间的视图控制器只是简单的remove掉。
样例
UIButton* presentButton = [UIButton buttonWithType:UIButtonTypeSystem];[presentButton setTitle:@"present下一个界面" forState:UIControlStateNormal];presentButton.frame = CGRectMake(100, 400, 150 , 70);[presentButton addTarget:self action:@selector(pressPresentButton) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:presentButton];
#import "PresentViewController.h"@interface PresentViewController ()@end@implementation PresentViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor whiteColor];UIButton* presentButton = [UIButton buttonWithType:UIButtonTypeSystem];[presentButton setTitle:@"dismiss到上一个界面" forState:UIControlStateNormal];presentButton.frame = CGRectMake(100, 300, 150 , 70);[presentButton addTarget:self action:@selector(pressPresentButton) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:presentButton];
}
- (void)pressPresentButton {[self dismissViewControllerAnimated:YES completion:nil];
}
git地址
点击此处