• [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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    请添加图片描述

    • 点击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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    请添加图片描述

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

    请添加图片描述

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

    总结

    使用

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

    特点

    • push由视图栈控制,每一个视图都入栈,调用之前的视图则需要出栈,可返回任意一层。也可以直接返回根视图
      push是由UINavigationController管理的视图控制器堆栈,在window下同时只能显示一个ViewController。
    • present弹出的视图是模态视图(我对模态视图的理解大概就是一个临时视图)
  • 相关阅读:
    智慧养殖方案:浅谈视频监控与AI智能识别技术助力奶牛高效、智慧养殖
    关于电影的HTML网页设计—— 电影小黄人6页 HTML+CSS+JavaScript
    深入理解操作系统之线程
    ASM之ClassWriter
    Java中有序单链表的构建
    jQuery常用API--元素操作
    刺激的8月!字节三面鞭尸/嘴贱痛失腾讯offer,想要个offer这么难吗
    Oracle-通过(RESTORE/RECOVER FROM SERVICE)方式搭建DataGuard
    利用uvicorn、Starlette和pipeline将一个训练好的大模型发布成一个web服务
    阿里巴巴架构实战:SpringBoot+SpringCloud+Docker+Nginx+分布式
  • 原文地址:https://blog.csdn.net/weixin_61639290/article/details/126896978