iOS ViewController 属性区分Presented and Presenting VC

在开发中,我们经常会去Present一个或多个ViewControllers, 然后我们会通过”.”取ViewController的presented或presenting的ViewController。每次都分不清楚,因此写个备忘。

官方定义

作为UIViewController的两个属性:

presentedViewController:

The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy. (read-only)
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.

presentingViewController:

The view controller that presented this view controller.

玄机

玄机就在TheThis, 仔细区分一下 the 和 this: the 指该属性指向的那个ViewController; This 指self,即拥有该属性的VC.

这样就比较清晰了。

1
2
3
4
5
self = instance Of UIViewController

self.presentedViewController = self 上面的present(modally)出来的那个viewController或者一个viewController簇中的根ViewControllers

self.presentingViewController = self下面的ViewController,self是被该VC present起来的.

最后配一张图说明下

后记

另外,说明一点题外话,多层present的VC调用self dismiss ,会dismiss 调self上面所有的present的VC。