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

3

9

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

Langsdon answered 13/5, 2013 at 8:47 Comment(0)
S
21

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.

Shackle answered 13/5, 2013 at 13:24 Comment(4)
From what I know the Container view controller is not restricted only to iPad but also available on iPhone link, I've used it in more than one app. In this case I want to do that on iPad.Langsdon
Anyway, the way to do it in the interface builder is through the Container View object in the Object Library! That will let you manage your child View Controller's view separatelyShackle
Thank you, I didn't know the existence of this object, actually I'm working with a single xib file instead of with a storyboard, and it seems be only available on storyboard. I going to try it...Langsdon
The Storyboard + Container View object solved my problem! I've updated the original answer.Langsdon
O
3

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

Orcus answered 5/12, 2013 at 18:51 Comment(0)
H
0

A very simple way to do it is just instantiate the child view controller by referencing its storyboard ID:

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

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

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

Hydrofoil answered 28/4, 2015 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.