Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "presentedViewController" in UIViewController mean?

In Apple's developer documentation, the property presentedViewController in UIViewController is described as "The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy". It is confusing. What does "its ancestors" mean?

like image 771
zhoudu Avatar asked Jul 21 '26 17:07

zhoudu


1 Answers

This question is really old, but it was still the top Google hit for my search, and the existing answer is wrong and makes things even more misleading.

This property always returns a presented view controller (or nil), it will never return an ancestor view controller. What the reference to ancestors means is that if the view controller you call it on isn't presenting any view controller itself then it will walk up the hierarchy to see if any parent view controllers are presenting anything.

For a concrete example - let's say you have ContentViewController which is a contained within a UINavigationController. The NavigtionController is currently presenting another view controller (perhaps a UIAlertViewController for example).

In this case the presentedViewController property of both the ContentViewController and the NavigationController will return the UIAlertController that is being presented by the NavigationController. When this is dismissed it will return nil for both of them.

So to add a bit more clarity to the line in the Apple Docs: "The view controller that is presented by this view controller, or [the view controller presented by] one of its ancestors in the view controller hierarchy."

like image 83
tangobravo Avatar answered Jul 26 '26 06:07

tangobravo