【iOS】push与present Controller的区别

文章目录


前言

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


一、push方法

objectivec 复制代码
    SecondViewController *second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];

二、pop方法

objectivec 复制代码
	//返回上一级
	[self.navigationController popViewControllerAnimated:YES];

	//返回根视图
	[self.navigationController popToRootViewControllerAnimated:YES];

	//返回指定级数 (objectAtIndex:参数为想要返回的级数)
	[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0]  animated:YES];

三、present方法

objectivec 复制代码
    SecondViewController *second = [[SecondViewController alloc] init];
    [self presentViewController:second animated:YES completion:nil];

四、dismiss方法

objectivec 复制代码
    [self dismissViewControllerAnimated:YES completion:nil];

五、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,我们可以用如下方法:

objectivec 复制代码
    UIViewController *rootVC = self.presentingViewController;
     while  (rootVC. presentingViewController ) {
              rootVC = rootVC.presentingViewController ;
             }
    [rootVC dismissViewControllerAnimated:YES completion:nil];

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

objectivec 复制代码
    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

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

objectivec 复制代码
    UIViewController *rootVC =  self.presentingViewController;
    while (![rootVC isKindOfClass:[SecondViewController class]])  {
        rootVC = rootVC.presentingViewController;
    }
    [rootVC dismissViewControllerAnimated:YES completion:nil];

动画

相关推荐
dnekmihfbnmv3 小时前
好用的电容笔有哪些推荐一下?年度最值得推荐五款电容笔分享!
ios·电脑·ipad·平板
黑果魏叔4 小时前
macOS Sequoia 正式版(24A335)黑苹果/Mac/虚拟机系统镜像
macos
tekin5 小时前
macos macport软件包管理工具 sudo port install xxx 安装的软件的路径 与 brew install xxx 软件安装路径总结
macos·brew·port·macport·port install·port软件包安装路径·brew软件包安装路径
Hellc0077 小时前
MacOS升级ruby版本
前端·macos·ruby
GEEKVIP13 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
逢生博客19 小时前
Mac 搭建仓颉语言开发环境(Cangjie SDK)
macos·华为·鸿蒙
Rverdoser1 天前
MacOS Catalina 从源码构建Qt6.2开发库之01: 编译Qt6.2源代码
macos
Magnetic_h1 天前
【iOS】单例模式
笔记·学习·ui·ios·单例模式·objective-c
归辞...1 天前
「iOS」——单例模式
ios·单例模式·cocoa
GEEKVIP1 天前
如何在没有备份的情况下恢复 Mac 上丢失的数据
经验分享·笔记·安全·macos·电脑·笔记本电脑·改行学it