[iOS界面切换- Present And Push]

前言

  • 写demo的时候经常用到界面的切换,今天深入了解了2种界面切换的不同之处,作揖记录

特点

  • pushViewController -导航栏方式切换界面,用 UINavigationController 的时候用 pushViewController切换视图,当使用Push的时候系统会自动在左上角带一个返回Back按钮,也可以自己使用pop返回
  • 请添加图片描述
  • presentViewController 是以模视图的方式弹出,可以理解为一种暂时存在的视图,弹出效果请添加图片描述
    的时候不会占据整个屏幕,可以设置为整个屏幕!,返回的时候可以下拉也可以设置按钮添加dismiss事件
  • 总的来说 present和push方法都可用于推出新的界面。 present和dismiss对应使用,push和pop对应使用

案例示意

  • 我创建了四个界面,界面一通过present到 界面二 ,在界面二设置一个nav nationController,配合push到界面三,在界面三使用diss或者pop返回进行尝试,push到界面四
界面一 - ViewController
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
// 点击button到界面二
@property (nonatomic, strong)UIButton* button;

@end

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_button setTitle:@"Next" forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(pressButtonNext) forControlEvents:UIControlEventTouchUpInside];
    _button.frame = CGRectMake(120, 200, 120, 40);
    [self.view addSubview:_button];
}
- (void)pressButtonNext {
    SecondViewController* ViewSecond = [[SecondViewController alloc] init];
    // 此处创建navgationcotroller,为了再界面二push到界面三
    ViewSecond.view.backgroundColor = [UIColor redColor];
    UINavigationController* navSecond = [[UINavigationController alloc] initWithRootViewController:ViewSecond];
    [self presentViewController:navSecond animated:YES completion:nil];
}

@end

请添加图片描述

  • 点击Next到界面二
SecondViewController
  • 设置一个ButtonNextTwo到界面三,一个backBUtton回到上一级,里面存放dismiss函数 [self dismissViewControllerAnimated:YES completion:nil];配合present
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SecondViewController : UIViewController
@property (nonatomic, strong)UIButton* buttonNextTwo;
@property (nonatomic, strong) UIButton* backButton;
@end

NS_ASSUME_NONNULL_END

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _buttonNextTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_buttonNextTwo setTitle:@"Next" forState:UIControlStateNormal];
    [_buttonNextTwo addTarget:self action:@selector(pressButtonNext) forControlEvents:UIControlEventTouchUpInside];
    _buttonNextTwo.frame = CGRectMake(120, 200, 120, 40);
    [self.view addSubview:_buttonNextTwo];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_backButton setTitle:@"BAck"forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(pressBack) forControlEvents:UIControlEventTouchUpInside];
    _backButton.frame = CGRectMake(120, 400, 120, 40);
    [self.view addSubview:_backButton];
    
}
- (void)pressButtonNext {
    ThirdViewController* thirdView = [[ThirdViewController alloc] init];
    thirdView.view.backgroundColor = [UIColor blueColor];
    [self.navigationController pushViewController:thirdView animated:YES];
    
}
- (void)pressBack {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

请添加图片描述

  • 可以在View3的点击button添加如下函数使充满整个屏幕
  • ViewSecond.modalPresentationStyle = UIModalPresentationFullScreen;
  • 接下来点击Next到View3
ViewControllerThird
pop返回效果
  • 由于采用了push推出界面,所以左上角自带了返回按钮,也可以自己设置返回按钮
  • Pop函数-push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈,可返回任意一层
    • 直接返回上一层
[self.navigationController popViewControllerAnimated:YES];
    • 返回根视图 (push推出的根视图)
self.navigationController popToRootViewControllerAnimated:YES];
    • 返回任意一层
[self.navigationController popToViewController://任意的push推出的界面
animated:YES];
  • BACK函数使用[self.navigationController popViewControllerAnimated:YES];效果
    • 点击到界面三请添加图片描述
    • 点击左上角自带Back或者Pop函数即可返回
界面三diss Miss返回到哪
  • 把back函数改一下
- (void)pressBack {
    // dismiss
    // diss回到第一个界面 ,因为dismiss和present互动,直接就到了第一个界面
    [self dismissViewControllerAnimated:YES completi
 }

请添加图片描述

  • 直接返回到了界面一,所以虽然是push推出的界面,配合dismiss使用也是找到present推出的界面返回!

总结

使用

  • push推出视图的时候配合创建创建了一个导航栏才能push 否则无效,返回的时候配合Pop函数或者自带的按钮
  • present推出视图配合dismiss函数返回

特点

  • push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈,可返回任意一层。也可以直接返回根视图
    push是由UINavigationController管理的视图控制器堆栈,在window下同时只能显示一个ViewController。
  • present弹出的视图是模态视图(我对模态视图的理解大概就是一个临时视图)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值