基本概念
在日常的iOS开发中经常会遇到切换弹出界面的操作,Present和Pop都是常用的方法,下面聊聊他俩的一些特点:
相同点
:都可以推出新的界面
不同点
:
1.present对应dismiss
,push和pop
对应。
2.present只能逐级返回,而push可以返回任意级的界面。
3.present一般用于不同业务界面的切换,push一般用于同一业务不同界面之间的切换
4.present弹出的视图是模态视图,push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈。
使用方法
开发中我们可能遇到两个视图控制器的切换,这里一般与UINavigationController导航栏控制器联合使用,有时为了某些效果又需要进行模态切换,即present。
present 对应 dismiss
[self presentViewController:navigitionController animated:YES completion:nil];
- (void)pressBack {
[self dismissViewControllerAnimated:YES completion:nil];
}
push对应pop
- (void)pushPress {
SeciondViewController* secondViewController = [[SeciondViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
}
这里注意当我们在导航栏使用push时,会在下一个界面自动生成一个back,可以返回push的这一个界面,如下所示:
最后时pop返回:
- (void)backPress {
// 返回上一级
[self.navigationController popViewControllerAnimated:YES];
// 返回根视图
[self.navigationController popToRootViewControllerAnimated:YES];
// 返回指定的界面
//objectAtIndex:参数为想要返回的级数
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
}
Demo代码地址
github:https://github.com/XUPTzjw/ViewController-and-present