iOS present 和 push

简介

  • 共同点
    – present和push方法都可用于推出新的界面。 present和dismiss对应使用,push和pop对应使用。

  • 不同点
    – present弹出的视图是模态视图(我对模态视图的理解大概就是一个临时视图);push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈
    – present只能逐级返回;push可返回任意一层

使用方法

  • 使用UINavigationController时使用push方法: 例如:[self.navigationController pushViewController:xxx animated:NO];
    返回时用pop: 例如:[self.navigationController popViewControllerAnimated:NO];

  • 其他时候用present方法: 例如:[self presentViewController:xxx animated:NO completion:nil]; ;
    返回时用dismiss: 例如:[self dismissViewControllerAnimated:NO completion:nil];

A视图 present 到 B视图再push到 C视图

为了在B视图里能push,我给B视图创建了一个导航栏,A视图 present进去
A视图按钮的点击事件:

- (void)pass {
    NSLog(@"press1");
    FirstViewController *first = [[FirstViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:first];
    [self presentViewController:nav animated:NO completion:nil];
}

B视图push到C视图
B视图按钮的点击事件:

- (void)pass {
    NSLog(@"press2");
    SecondViewController *second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:NO];
}

C视图使用dismiss返回

- (void)back {
    NSLog(@"back");
    [self dismissViewControllerAnimated:NO completion:nil];
}

C视图:
在这里插入图片描述
点击back后:
在这里插入图片描述
直接返回到了A视图!

C视图使用pop返回

- (void)back {
    NSLog(@"back");
    [self.navigationController popViewControllerAnimated:NO];
}

点击back后:
在这里插入图片描述
则返回到了B视图!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值