Storyboards and custom container view controllers

Go To StackoverFlow.com

16

I'm creating a custom container view as per the apple spec. I would like to use the storyboard to connect three static child UIViewControllers. Is there an easy way in the storyboard to connect via a Relationship as seen for the UINavigationController in the storyboard?

NavigationController 'relationship'

Based on my research, it seems like this isn't possible.

2012-04-05 14:58
by Stephen
Any joy? I was just about to post the same question.. - jackslash 2012-05-07 15:52
I think it's not possible. I've succumbed to setting up any custom containers programatically - Stephen 2012-05-07 19:52
Ok, thanks. I went with using xibs the old fashioned way for now, in the future i think ill design custom segues like in your article - jackslash 2012-05-08 10:23
My research also suggests it is not possible at this time. Sigh - Joe D'Andrea 2012-06-20 17:57
it IS NOT POSSIBLE LITERALLY on the storyboard, but it's very easy: there are two easy methods at launch time: http://stackoverflow.com/questions/23597123/in-fact-is-destinationviewcontroller-in-storyboard-order and http://stackoverflow.com/questions/15705555/access-to-container-view-controller/15706092#1570609 - Fattie 2014-05-11 20:15


5

It IS possible to link a container view controller to a child. In fact, it's trivially easy to do so. You bring up the Object library, type "Container" into the search field, and look for the object "Container view". It looks like this:

enter image description here

Drag a container view into your view controller's content view.

Then you control-drag from the container view onto the other view controller that you want the container view to host. IB sets up an "embed segue" for you. The embed segue gets invoked when the parent view controller's content view is loaded. The embed segue sets up the parent/child view controller relationship and does the housekeeping you need. It's easy and painless.

Your prepareForSegue method is called for each embed segue. You can assign unique identifiers to your embed segues just like other segues, and then use the segue ID in your prepareForSegue to do extra setup for the child view controller.

Take a look at this project on github that shows how to use embed segues to include 2 static UITableViewControllers in a parent using container views and embed segues. This project sets up custom protocols for the parent and child VCs to communicate with each other. In the prepareForSegue method the parent saves pointers to both child VCs, and also sets itself up as delegates of both child VCs so the child can communicate back to the parent.

You can find the project at this link: https://github.com/DuncanMC/test

2014-05-25 19:28
by Duncan C
Who down voted my answer and why? If you think my answer is wrong or somehow poor, please say where you think my answer faile - Duncan C 2014-08-21 02:01
Hi Duncan, you've explained how to use container views. Note that in your last paragraph you say "the prepareForSegue method the parent saves pointers to both child VCs". That is THE WHOLE POINT of this question. Note that you CANNOT DO THAT on the storyboard - which is ridiculous, Apple messed-up. As you show in your demo project, you have to do it in code - Fattie 2015-09-20 18:31
Doesn't seem like a big issue to me. The prepareForSegue method is a clean, easy place to wire up your connections. Another possibility is that view controllers have a parentViewController property that is either nil or points to a VC's parents, and VCs also have a childViewControllers property that holds an array of a parent view controller's children. Those two things give you access to your parent and child/children, although if you have multiple child view controllers you need a way to tell them apart - Duncan C 2015-09-20 20:57
hi Duncan, there may be some confusion. The very nature of the question, here is, asking precisely that thing. As you beautifully showed in your example project, you have to get the reference in code, Apple has not supplied an IBOutlet -like system to get the reference - Fattie 2015-09-20 21:35


1

You can use Container Views for it. Container View creates relationship to the new view controller automatically.

2013-07-10 11:14
by Sviatoslav Yakymiv
OP is asking how to then connect the controllers - Fattie 2014-05-11 20:14
If he uses container view, controller will be connected automatically - Sviatoslav Yakymiv 2014-05-12 16:32
No, it is not. Just try it. It could be we are talking about different things. Just follow the directions here: http://stackoverflow.com/a/23403979/294884 the VIEW CONTROLLER is not available in any way. You must use one of the two methods mentioned above - Fattie 2014-05-12 16:46
@JoeBlow, you are wrong. Container views will host child view controllers. It is very easy. See my answer - Duncan C 2014-05-25 19:29
Hi Duncan, I will definitely check out the example project! I saw you earlier mention the "object" but I was not able to end up with an outlet vc (in the parent vc) connected to the child vc. Let me check out your project, thanks! - Fattie 2014-05-25 19:33


0

Have you tried subclassing the UITabbarcontroller or UINavigationController?

You can create your custom class extending from one of these and then set it in your storyboard - thus allowing you to create the same relationships. Then you can hide the tabbar and add whatever functionality you want.

enter image description here

2013-04-11 22:02
by HarrisonJackson


-1

I haven't done this but it's an interesting problem. Did you define your child view controllers in your container controller before trying associate them in the storyboard (you may be doing this - no code is shown)? From the docs it sounds like that is what you need to do - it may be that it is not like with the generic classes where you can just drag them in.

In order for iOS to route events properly to child view controllers and the views those controllers manage, your container view controller must associate a child view controller with itself before adding the child’s root view to the view hierarchy.

Anyway - a guess...

2012-04-13 22:39
by No Grabbing
The issue I'm having is that Apple allows UITabBarController and UINavigationController to have these special Relationship properties which custom objects don't have. I have child view controllers in the storyboard as part of the controller graph, but no amount of cntl-clicking allows me to wire them up - Stephen 2012-04-19 22:34
Isn't that a bug - Ricardo 2012-04-20 12:52
Stephen - exactly. The only thing I see with a vanilla container view is an option to "embed" exactly ONE other view controller. Just one. Not two or more. That's the part that has me scratching my head. It's a container, after all, so what good is embedding just one? : - Joe D'Andrea 2012-06-20 17:58
Instead of the embed segue you could use multiple custom segues, take a look here: http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controller - brainray 2014-01-13 15:09
Ads