【iOS】Present和Push的区别

简介

  • pushpresent都可以推出新的界面。
  • presentdismiss对应,pushpop对应。
  • 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的界面中,我们点击了当前页面上的某个控件,让显示第ntabBar上的内容,相当于从一个tabBar1跳转另一个tabBar(n),其实这个很简单,在当前tabBar1界面控件的事件中加一行代码即可

self.tabBarController.selectedIndex = 2; //(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地址

点击此处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值