uiviewcontrollerinterface-builderuistoryboardseguecontainer-viewchildviewcontroller

How to create a container/child viewcontroller relationship in interface builder


To create from code a parent/child relationship between 2 view controllers basically it's just matter of doing something like:

[self addChildViewController:childViewController];
[self.view addSubview:childViewController.view];

where self is the parent view controller.
But, what if I want to create the same relationship completely from Interface Builder?
Or in other words: is there a way to re-create the behavior of the method addChildViewController using Interface Builder?
I didn't find a lot of documentation about that, here is an old unresolved post about the topic: https://devforums.apple.com/message/455758#455758

Without properly setting up the addChildViewController relationship, none of the rotation methods are forwarded to my child view controller, here where my question come from.

This is what I've done in IB:

  1. drag and dropped a "View Controller" object from the "Object Library" panel into the "Objects" panel
  2. in the identity inspector I've changed its class to my UIViewController subclass ("Items View Controller")
  3. connected the view outlet to the controller
  4. connected all the other required outlets to the controller (List name, Table View)

The first "View" object in the picture is the view of my parent view controller, instead the highlighted "View" is the view of the child view controller ("Item View Controller").

enter image description here

The container controller also retain its child instance through an additional IBOutlet:

@property (nonatomic, strong) IBOutlet ItemsViewController *itemsViewController;

Thanks


Update 1: If I manually set the parent/child relationship in viewDidLoad of the container controller, all the rotation methods are correctly forwarded to the child.

[self addChildViewController:self.itemsViewController];

But I don't really know if this is the correct way of doing that, since I would like to do all using IB.


Update 2: Thanks to @micantox for his hint to use the "Container View" in the Object Library, I have converted my xib file to a Storyboard and now the child view controller is added to its parent, so I don't have to add it manually from code with addChildViewController and the rotation methods are forwarded as expected.
"Container View" basically implements the embed segue and is supported only from iOS 6.
This is an updated screenshot from my Storyboard:

enter image description here


Solution

  • The right way of creating container views for child view controllers is through the use of the object "Container View" in the Object Library. Dragging one in your View Controller's scene will create a new scene for the child view controller that can be managed separately from the the parent view controller.