Storyboards and custom container view controllers
Asked Answered
T

4

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.

Tales answered 5/4, 2012 at 14:58 Comment(5)
Any joy? I was just about to post the same question...Sheet
I think it's not possible. I've succumbed to setting up any custom containers programatically.Tales
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.Sheet
My research also suggests it is not possible at this time. Sigh.Settee
it IS NOT POSSIBLE LITERALLY on the storyboard, but it's very easy: there are two easy methods at launch time: #23597623 and #15706055Coinsure
C
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

Choosy answered 25/5, 2014 at 19:28 Comment(4)
Who down voted my answer and why? If you think my answer is wrong or somehow poor, please say where you think my answer failedChoosy
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.Coinsure
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.Choosy
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.Coinsure
A
1

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

Ables answered 10/7, 2013 at 11:14 Comment(5)
OP is asking how to then connect the controllers.Coinsure
If he uses container view, controller will be connected automatically.Ables
No, it is not. Just try it. It could be we are talking about different things. Just follow the directions here: https://mcmap.net/q/17273/-how-to-add-a-subview-that-has-its-own-uiviewcontroller-in-objective-c the VIEW CONTROLLER is not available in any way. You must use one of the two methods mentioned above.Coinsure
@JoeBlow, you are wrong. Container views will host child view controllers. It is very easy. See my answer.Choosy
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!!Coinsure
S
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

Susannasusannah answered 11/4, 2013 at 22:2 Comment(0)
M
-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...

Mendez answered 13/4, 2012 at 22:39 Comment(3)
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.Tales
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? :\Settee
Instead of the embed segue you could use multiple custom segues, take a look here: sandmoose.com/post/35714028270/…Lamina

© 2022 - 2024 — McMap. All rights reserved.