当前位置: 移动技术网 > 移动技术>移动开发>IOS > [iOS开发] present 和 push

[iOS开发] present 和 push

2020年09月22日  | 移动技术网移动技术  | 我要评论
一、基本概念共同点– present和push方法都可用于推出新的界面。 present和dismiss对应使用,push和pop对应使用。不同点– present弹出的视图是模态视图(我对模态视图的理解大概就是一个临时视图);push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈– present只能逐级返回;push可返回任意一层二、使用方法用UINavigationController的时候使用pushViewController:animated----返回之前的视图[

一、基本概念

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

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

二、使用方法

  1. 用UINavigationController的时候使用pushViewController:animated
    ----返回之前的视图[[self navigationController] popViewControllerAnimated:YES];
    —ps:push以后会在navigation的left bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。

  2. 其他时候用presentModalViewController:animated
    [self presentModalViewController:controller animated:YES];//YES有动画效果
    -----返回之前的视图 [self dismissModalViewControllerAnimated:YES];

三、页面的push/present的demo

给second界面创建一个导航栈控制器 可以保证等会push的实现
第一个界面present第二个界面

//第一个界面的按下跳转 使用present
-(void)press{
    SecondViewController *secondView = [[SecondViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:secondView];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
}

第二个界面push第三个界面

//第二个界面的返回按钮与按下push一个新的界面
-(void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)push{
    ThirdViewController *thirdView = [[ThirdViewController alloc]init];
    [self.navigationController pushViewController:thirdView animated:YES];
}
//第三个界面的back 如果是dismiss那么就返回第一个界面 如果是pop那么就是第二个界面
- (void)back{
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

在这里插入图片描述

本文地址:https://blog.csdn.net/m0_46110288/article/details/108740633

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网