How to create a container / child view manager relationship in an interface builder

To create a parent / child relationship between two view managers from code, this basically matters:

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

where self is the parent view controller.
But, what if I want to completely create the same relationship from Interface Builder?
Or, in other words: is there a way to recreate the behavior of the addChildViewController method using Interface Builder? I did not find much documentation about this, here is an old unresolved article about the topic: https://devforums.apple.com/message/455758#455758

Without properly setting the addChildViewController relationship, none of the rotation methods are redirected to my child view controller, here, where my question arises.

This is what I did at IB:

  • drag the View Controller object from the Object Library panel to the Objects panel
  • in the identity inspector I changed my class to my subclass of UIViewController ("Element Presentation Controller")
  • connected the outlet to the controller.
  • connected all other required outputs with the controller (list name, table view)

The first “View” object in the image 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 saves the child instance through an optional IBOutlet:

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

thanks


Update 1: If I manually set the parent / child relationships in the viewDidLoad of the container controller, all rotation methods will be correctly redirected to the child.

 [self addChildViewController:self.itemsViewController]; 

But I really do not know if this is being done correctly, since I would like to do everything with IB.


Update 2: Thanks to @micantox, for his help using the "View container" in the object library, I converted my xib file to Storyboard and now the child view controller is added to its parent, so I don’t need to add it manually from the code using addChildViewController. and rotation methods are redirected as expected.
"Container View" mainly implements embed segue and is only supported with iOS 6.
This is an updated screenshot from my storyboard:

enter image description here

+7
source share
3 answers

The correct way to create container views for child controllers is to use the View Container object in the object library. Dragging one of the scenes into the View Controller will create a new scene for the child view controller, which can be controlled separately from the parent view controller.

+18
source

Also, if you are trying to create an inline segue in an existing view controller in your storyboard, drag control from the container view, not the container VC, to the VC you want to embed.

+2
source

A very simple way to do this is to simply create an instance of the child view controller by specifying its storyboard identifier:

 UITableViewController *childViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"childViewController"]; [self addChildViewController:childViewController]; [self.view addSubview:childViewController.view]; 

This way you can build both view controllers in the interface builder. You can set the ID of the storyboard in the interface builder: select the view controller, and you will see a field for it in the identity inspector.

0
source

All Articles