presentingViewController和presentedViewController以及dismissViewControllerAnimated的使用

文章的由来:

偶然在一次想要判断当前控制器是通过present模态推出还是通过push入栈显示的时候注意到了presentingViewController和presentedViewController两个属性,即:如果presentingViewController不为空,则当前控制器是通过present模态推出的方式显示的,那么相应的返回方式应该是dismiss而不是pop,之后又在头文件看到还有一个presentedViewController,就研究了下这两个属性的具体意义

文档中的presentingViewController和presentedViewController
presentingViewController

The view controller that presented this view controller.
When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.

大意是:呈现此视图控制器的视图控制器。
当您使用presentViewController:animated:completion: 方法(显式或隐式地)呈现一个视图控制器时,呈现的视图控制器将这个属性设置为呈现它的视图控制器。如果视图控制器不是以模态【present】呈现的,但是它的上一级是,这个属性包含呈现上一级的视图控制器。如果当前视图控制器或它的任何上一级都没有以模态方式显示,则此属性中的值为nil。

举例说明:

有五个控制器 ABCDE,应用启动先显示根控制器A,之后A通过present方式推出带导航栏的B,B再通过push的方式推出C,C再通过push的方式推出D,D再通过push的方式推出E,那么E的 presentingViewController 就是 B
也就是presentingViewController即为当前控制器的上级控制器【即父级控制器】,并且是通过present方式显示的父级,上面例子中只有B是通过present方式推出的,且B是CDE的父级,那么 D 的presentingViewController也将是B,大家可以自己理解一下
同理:如果还是有五个控制器 ABCDE,应用启动先显示根控制器A,之后全部是通过present方式依次推出 B、C、D、E,那么,这个时候E的presentingViewController就是D了,D的presentingViewController就是C

哈哈哈,会不会说的有点多,其实自己看看概念慢慢想就会想通的,后面会再介绍一下这个属性有什么用,会用在哪种场景

presentedViewController

The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.
When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that called the method has this property set to the view controller that it presented. If the current view controller did not present another view controller modally, the value in this property is nil.

大意是:视图控制器由该视图控制器呈现,或其在视图控制器层次结构中的祖先之一。
当您使用presentViewController:animated:completion: method方法以模态(显式或隐式)方式呈现一个视图控制器时,调用该方法的视图控制器将这个属性设置为它所呈现的视图控制器。如果当前视图控制器没有以模式显示另一个视图控制器,则此属性中的值为nil。

举例说明:

有两个控制器AB,A通过present模态推出B,那么A的presentedViewController就是B,也就是你通过present模态推出了谁,你的presentedViewController就是谁,这样就好理解了,哈哈,这个属性就是这么简单,当然,如果没有模态推出任何控制器,那么属性值就是nil

嗯,剩下一个dismissViewControllerAnimated这个方法解释完,就不会愧对我的标题了

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值