• 【iOS】push与present Controller的区别



    前言

    iOS推出与退出界面有两种方式——push与present,接下来笔者分别介绍这两种方式


    一、push方法

        SecondViewController *second = [[SecondViewController alloc] init];
        [self.navigationController pushViewController:second animated:YES];
    
    • 1
    • 2

    二、pop方法

    	//返回上一级
    	[self.navigationController popViewControllerAnimated:YES];
    
    	//返回根视图
    	[self.navigationController popToRootViewControllerAnimated:YES];
    
    	//返回指定级数 (objectAtIndex:参数为想要返回的级数)
    	[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0]  animated:YES];
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    三、present方法

        SecondViewController *second = [[SecondViewController alloc] init];
        [self presentViewController:second animated:YES completion:nil];
    
    • 1
    • 2

    四、dismiss方法

        [self dismissViewControllerAnimated:YES completion:nil];
    
    • 1

    五、dismiss多级的方法

    presentedViewController和presentingViewController是UIViewController里面的两个属性
    PresentedViewController 与 PresentingViewController区别

    presentedViewController:The view controller that was presented by this
    view controller or its nearest ancestor. 由这个视图控制器或它最近的祖先呈现的视图控制器
    presentingViewController:The view controller that presented this view
    controller (or its farthest ancestor.) 呈现此视图控制器(或其最远祖先)的视图控制器。

    假设从A控制器通过present的方式跳转到了B控制器,那么 A.presentedViewController 就是B控制器;
    B.presentingViewController 就是A控制器;


    举例

    创建四个页面ABCD,例如我们想从D->A,我们可以用如下方法:

        UIViewController *rootVC = self.presentingViewController;
         while  (rootVC. presentingViewController ) {
                  rootVC = rootVC.presentingViewController ;
                 }
        [rootVC dismissViewControllerAnimated:YES completion:nil];
    
    • 1
    • 2
    • 3
    • 4
    • 5

    例如我们想从D->B,我们可以使用presenting来跳转页面

        [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    
    • 1

    如下代码也可以实现D->B

        UIViewController *rootVC =  self.presentingViewController;
        while (![rootVC isKindOfClass:[SecondViewController class]])  {
            rootVC = rootVC.presentingViewController;
        }
        [rootVC dismissViewControllerAnimated:YES completion:nil];
    
    • 1
    • 2
    • 3
    • 4
    • 5

    动画

    在这里插入图片描述

  • 相关阅读:
    管理区解耦架构见过吗?能帮客户搞定大难题的
    区域运营推广策划案
    华为设备配置Smart Link主备备份
    c++取经之路(其八)——基础模板
    FFplay文档解读-14-输入设备二
    程序和进程
    mysql的数据表同步工具 canal的使用
    django开源电子文档管理系统_Django简介、ORM、核心模块
    python笔记 -- 面向对象
    Python抽象类和接口类
  • 原文地址:https://blog.csdn.net/weixin_72437555/article/details/132817871