【iOS】push,pop和present,dismiss

文章目录

前言

在之前的学习中,我们发现iOS有两种用于推出新界面的常用方法,分别是push和present,但是二者存在很多区别

present只能返回自己的上一级视图,而push的所有视图都是由视图栈控制,可以返回上一级,也可以返回根视图或者其他视图

在iOS13之后,我们present推出的页面不会完全覆盖之前的界面,上面会留有一条缝隙,并且我们可以通过向下拖动直接关闭当前的页面

push和pop

push和pop用于在导航控制器UINavigationController中,对视图控制器的添加与移除

原理是导航控制器会维护一个栈,在经过push后,新的view会被压到栈顶,所以在删除或者添加时要先对栈顶的进行操作

对根视图进行pop时是一个无效的操作,除非用setViewControllers: 直接替换整个栈或者把整个 UINavigationController dismiss 掉。

  • pushViewController: animated:

    就是把一个新的视图控制器压入栈顶,导航控制器会显示这个新的控制器。

    (相当于进栈 → 显示新页面)

  • popViewControllerAnimated:

    就是把当前栈顶的控制器移除,导航控制器会显示下一个栈顶(之前的那个控制器)。

    (相当于出栈 → 回到上一个页面)

与此同时,除了对单个的push和pop,你也可以用以下两个方法

  • popToRootViewControllerAnimated: 一次性回到根控制器
  • popToViewController:animated: 一次性回到栈中某个已存在的控制器

若你直接pop到根视图或者下面的某个指定视图控制器,会将其上面的全部释放

部分代码演示:

首先,我们要先设置一个根视图控制器

objc 复制代码
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    self.window.frame = [UIScreen mainScreen].bounds;
    VCRoot* root = [[VCRoot alloc] init];
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController: root];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
}

之后我们再在根视图上添加跳转函数及对根视图的初始化

objc 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"First";
    self.navigationItem.title = @"根视图";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle: @"下一级界面" style: UIBarButtonItemStylePlain target: self action: @selector(pressNext)];
    self.navigationItem.rightBarButtonItem = next;
}

- (void)pressNext {
    VCSecond* vcsecond = [[VCSecond alloc] init];
    [self.navigationController pushViewController: vcsecond animated: YES];
}

后面几个视图也如法炮制,并在最后一个视图设置跳转到根视图的方法

运行结果:

present和dismiss

present和dismiss分别用来从当前控制器模态弹出视图控制器和关闭模态视图控制器,两个方法分别为

模态弹出的意思是,一个新的页面(控制器)以覆盖的形式展示在当前页面之上,用户必须 处理完 / 关闭这个页面(通过 dismiss),才能回到之前的页面

基本方法

  • presentViewController:

    使用其弹出一个视图之后,当前控制器会持有一个presentedViewController,被展示的控制器(B)会有一个 presentingViewController 指向 A。

  • dismissViewController:

    默认只能 dismiss 自己或自己上层的控制器,不能直接跨级 dismiss。

属性说明

这里提到了两个之前未接触的属性,presentedViewController和presentingViewController

  • presentedViewController:

    当前控制器所呈现的控制器 A.presentedViewController = B

  • presentingViewController:

    当前控制器的呈现者 B.presentingViewController = A

常见的用法

  1. A -> B的跳转
objc 复制代码
SecondViewController *second = [[SecondViewController alloc] init];
[self presentViewController:second animated:YES completion:nil];

所产生的效果:

  • A present出 B
  • A.presentedViewController = B
  • B.presentingViewController = A
  1. B -> A的返回
objc 复制代码
[self dismissViewControllerAnimated:YES completion:nil];

即收起b返回a

  1. 多级返回:D -> A

假设层级是:A -> B -> C -> D如果想直接从 D 返回到 A,可以找到最顶层的 presentingViewController:

objc 复制代码
UIViewController *rootVC = self.presentingViewController;
while (rootVC.presentingViewController) {
    rootVC = rootVC.presentingViewController;
}
[rootVC dismissViewControllerAnimated:YES completion:nil];
  1. 多级返回:D -> B

若想从D返回到B,我们可以走两次presenting,即一次一次退出:

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

亦或者我们可以先找到指定类再 dismiss

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

这样我们可以指定返回到某个具体页面

运行演示

push和present区别

  • push必须嵌套于导航控制器,且原理是把新控制器压入导航栈,主要用于一层一层的层级导航,且自动有返回按钮方便返回
  • present可在任何控制器上使用,且不会完全覆盖,返回时除了下滑必须手动添加按钮进行关闭,主要适用于独立的弹出提示等放面
相关推荐
00后程序员张12 分钟前
iOS App 混淆实战,在源码不可用情况下的成品加固与测试流程
android·ios·小程序·https·uni-app·iphone·webview
i建模13 分钟前
鸿蒙与iOS跨平台开发方案全解析
ios·华为·harmonyos
运维行者_1 小时前
OpManager 与 iOS 26:开启 IT 运维新时代
运维·网络·网络协议·网络安全·ios·iphone·告警
Digitally2 小时前
如何将三星手机的照片传输到Mac——6种可行的方法
macos·智能手机
YungFan4 小时前
iOS26适配指南之UIScrollView
ios·swift
2501_916013744 小时前
iOS 26 设备文件管理实战指南,文件访问、沙盒导出、系统变更与 uni-app 项目适配
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_915921434 小时前
前端用什么开发工具?常用前端开发工具推荐与不同阶段的选择指南
android·前端·ios·小程序·uni-app·iphone·webview
学习研习社4 小时前
移动硬盘上的文件消失了?以下是Mac电脑解决方法
macos·电脑
2501_916007474 小时前
iOS 26 能耗检测实战指南,升级后电池掉速是否正常 + KeyMob + Instruments 实时监控 + 优化策略
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916013745 小时前
苹果上架 App 全流程详解,iOS 应用发布步骤、ipa 文件上传工具、TestFlight 测试与 App Store 审核经验
android·ios·小程序·https·uni-app·iphone·webview