【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];

动画

相关推荐
酒醉的胡铁1 小时前
uniapp解决video组件在ios上全屏页面旋转90度,组件旋转180度
ios·uni-app
TheNextByte12 小时前
将照片从Mac传输到Android 7 种可行方法
android·macos·gitee
张飞签名上架3 小时前
深度解析超级签:iOS 应用分发的便捷之选与风险权衡
ios·苹果签名·企业签名·苹果超级签名·tf签
提笔忘字的帝国3 小时前
【2026版】macOS 使用 Homebrew 快速安装 Java 21 教程
java·开发语言·macos
2501_915918413 小时前
iOS App的tcp、udp数据包抓取在实际开发中的使用方式
android·tcp/ip·ios·小程序·udp·uni-app·iphone
洪大宇5 小时前
Mac 使用蓝牙功能
macos
2501_915909065 小时前
iOS 应用在混淆或修改后,如何完成签名、重签名与安装测试
android·ios·小程序·https·uni-app·iphone·webview
Digitally8 小时前
如何顺利地将手机号码转移到新iPhone
ios·iphone
Benny的老巢15 小时前
Mac上用XAMPP搭建局域网可访问的开发环境,让局域网内其他设备通过域名访问
nginx·macos·apache·xampp·php开发环境
2501_941881401 天前
在墨西哥城复杂流量环境下构建高稳定性API网关的架构设计与实现实践分享
macos·golang·xcode