首页 > 代码库 > View Controller Relationships

View Controller Relationships

Parent-child relationships
Parent-child relationships are formed when using view controller containers. Examples of view
controller containers are UINavigationController, UITabBarController, and UISplitViewController. 

You can identify a view controller container because it has a
viewControllers property that is an array of the view controllers it contains.

 

View controllers in a parent-child relationship form a family. So, a UINavigationController and its
viewControllers are in the same family. A family can have multiple levels. For example, imagine a
situation where a UITabBarController contains a UINavigationController that contains a
UIViewController. These three view controllers are in the same family. The container
classes have access to their children through the viewControllers array, and the children have access
to their ancestors through four properties of UIViewController.

 

Every UIViewController has a parentViewController property. This property holds the closest view
controller ancestor in the family. Thus, it could return a UINavigationController,
UITabBarController, or a UISplitViewController depending on the makeup of the family tree.
The ancestor-access methods of UIViewController include navigationController,
tabBarController, and splitViewController. When a view controller is sent one of these messages,
it searches up the family tree (using the parentViewController property) until it finds the appropriate
type of view controller container. If there is no ancestor of the appropriate type, these methods return
nil.

 

Presenting-presenter relationships

The other kind of relationship is a presenting-presenter relationship, which occurs when a view
controller is presented modally. When a view controller is presented modally, its view is added on
top of the view controller’s view that presented it. This is different than a view controller container,
which intentionally keeps a spot open on its interface to swap in the views of the view controllers it
contains. Any UIViewController can present another view controller modally.

There are two built-in properties for managing the relationship between presenter and presentee. A
modally-presented view controller’s presentingViewController will point back to the view
controller that presented it, while the presenter will keep a pointer to the presentee in its
presentedViewController property.

 

  • First, notice that the properties for parent-child relationships can never cross over family boundaries. 
  • When a view controller is presented modally, the actual presenter is the oldest member of the presenting family.

This behavior explains why the BNRDetailViewController obscures the UINavigationBar when
presented modally but not when presented normally in the UINavigationController’s stack. Even
though the BNRItemsViewController is told to do the modal presenting, its oldest ancestor, the
UINavigationController, actually carries out the task. The BNRDetailViewController is put on top of

the UINavigationController’s view and thus obscures the UINavigationBar.

You can actually override this oldest-ancestor behavior (but only on the iPad). By doing so, you can
specify where the views of the presented view controller family appear on the screen. For example,
you could present the BNRDetailViewController and its navigationController so that it only
obscures the UITableView but not the UINavigationBar. 

Every UIViewController has a definesPresentationContext property for this purpose. By default,
this property is NO, which means the view controller will always pass presentation off to its next
ancestor, until there are no more ancestors left. Setting this property to YES interrupts the search for
the oldest ancestor, allowing a view controller to present the modal view controller in its own view. 

Additionally, you must set the modalPresentationStyle for the presented view
controller to UIModalPresentationCurrentContext.

  • Notice also that the presentingViewController and presentedViewController are valid for every

view controller in each family and always point to the oldest ancestor in the other family.

View Controller Relationships