iOS转场之present与dismiss的使用
present的使用方式
present只能是A present B , B present C , C present D这样的链式弹出。
不能A present B , A present C , A present D这样的叠加弹出,会报错。
dismiss的使用方法
dismiss是底部父VC的方法,由父类批量dismiss掉它所有的链式弹出的子VC,
如果在子类调用dismiss,它会关闭它自己,如D调用dismiss,D会退出,如果在中间调用dismiss,它会关闭它上面的批量部分,如C调用dismiss,C、D会退出。
presentedViewController属性与presentingViewController属性
UIViewController下presentedViewController属性和presentingViewController属性是用来表示A present B , B present C , C present D这样的链式弹出关系的。
它们表示的意义是:
presentedViewController是底部VC属性,表示下面VC拥有presentedViewController被弹出的VC。
presentingViewController是顶部VC属性,表示上面VC拥有presentingViewController弹出它的VC。
定义如下:
1 2 3 4 | / / The view controller that was presented by this view controller or its nearest ancestor. @property (nullable, nonatomic,readonly) UIViewController * presentedViewController API_AVAILABLE(ios( 5.0 )); / / The view controller that presented this view controller ( or its farthest ancestor.) @property (nullable, nonatomic,readonly) UIViewController * presentingViewController API_AVAILABLE(ios( 5.0 )); |
举例如下:
A present B时,A页面在下面,B页面在上面
1 2 3 4 5 6 7 8 9 10 11 12 | - - - - - - - - - - - - - - - - - - - - - A弹B后 - - - - - - - - - - - - - - - - - - - - - A.presentingViewController (null) A.presentedViewController B B.presentingViewController A B.presentedViewController (null) - - - - - - - - - - - - - - - - - - - - - B弹C后 - - - - - - - - - - - - - - - - - - - - - A.presentingViewController (null) A.presentedViewController B B.presentingViewController A B.presentedViewController C C.presentingViewController B C.presentedViewController (null) |
常见的问题场景
问题1:业务VC要present出SDK中的功能VC, SDK功能VC要present出子功能VC,当点击SDK中的功能VC的“X”或者子功能VC的“X”,都要让SDK页面完全退出。
简化问题为A present B , B present C 。关闭C时,BC要退出;关闭B时,BC要退出。
对于关闭C时,BC要退出;关闭B时,BC要退出需求可以调用B.presentedViewController dissmiss方法解决。
· 分享4款.NET开源、免费、实用的商城系统
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了