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

动画

相关推荐
0xAaron4 小时前
确定crash文件和dSYM是否对应
ios·uuid·crash·dsym
0xAaron6 小时前
符号表和 dSYM UUID 确认
ios·cocoa·uuid·符号表·dsym
0xAaron6 小时前
如何使用dSYM文件来符号化崩溃信息
ios·swift·调试·崩溃·符号化·dsym
小蜜蜂嗡嗡6 小时前
【flutter项目从xcode运行时报错:Undefined symbol: _OBJC_CLASS_$_WeiboSDK】
flutter·cocoa·xcode
Sheffi666 小时前
RunLoop 深度探索:线程为什么不会自动退出
macos·objective-c·cocoa
2501_915918418 小时前
Flutter 加固方案全解析,从 Dart 层到 IPA 成品的多工具协同防护体系
flutter·macos·ios·小程序·uni-app·cocoa·iphone
碧水澜庭9 小时前
Mac升级集成Gemini的Chrome浏览器完整指南
chrome·macos·ai编程
wsxlgg9 小时前
IOS 打包上传提示you do not have required contracts to perform an operation
ios
每周报刊9 小时前
初代 iPhone SE 谢幕:被标为 “过时”,小屏旗舰时代彻底落幕
ios·iphone
RollingPin9 小时前
iOS 动态库与静态库的区别
ios·framework·动态库·静态库·符号表·三方库·dyld