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

动画

相关推荐
丁总学Java2 小时前
在 Mac ARM 架构的 macOS 系统上启用 F1 键作为 Snipaste 的截屏快捷键
macos·snipaste
天下皆白_唯我独黑6 小时前
brew Nushell mac升级版本
macos
刘什么洋啊Zz10 小时前
MacOS下使用Ollama本地构建DeepSeek并使用本地Dify构建AI应用
人工智能·macos·ai·ollama·deepseek
goodmao10 小时前
【DeepSeek】-macOS本地终端部署后运行DeepSeek如何分析图片
gpt·macos·大模型·ollama·deepseek·本地图片分析
openinstall全渠道统计16 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
Macdo_cn16 小时前
Infuse Pro for Mac v8.1 全能视频播放器 支持M、Intel芯片
macos·音视频
软件技术NINI19 小时前
Deepseek本地部署指南:在linux服务器部署,在mac远程web-ui访问
linux·服务器·macos
早起的年轻人20 小时前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
貂蝉空大1 天前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎11 天前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve