I'm trying to use storyboard and get things working properly. I've added a a Container View to one of my existing views. When I try to add a reference to this in my view controller .h
file (ctrl-drag), I get a IBOutlet UIView *containerView
. How do I get a reference to the container view's view controller instead? I need the container view controller so I can set it's delegate to my view's controller so they can "talk" to each other.
I have my story board setup as:
And its referenced in my .h file as:
Notice in the .h that is is a UIView, not my InstallViewController for the view. How do I add a reference to the view controller? I need to be able to set its delegate.
The answer of Vitor Franchi is correct but could be more performant and convenient. Especially when accessing the child view controller several times.
Create a readonly property
Then create a convenient getter method
From now on access the child view controller that way
Will give you the UIView inside of the UIViewController that your controller UIView references. You can cast the subview to any type that inherits from UIView.
When you add a container view the xcode calls the UIViewController method
addChildViewController:
In your case, you can get the container ViewController looking for it on the SplashViewController's list of
childViewControllers
, something like this:I had the same doubt yesterday :)
There is another solution by specifying an identifier for the embed segue(s) and retrieve the corresponding view controllers in method
prepareForSegue:
The advantage of this way is that you needn't rely on a specific order in which your child view controllers are added due to the fact that each child view controller is embedded via an unique segue identifier.
Update 2013-01-17 - Example
c_SegueIdEmbedMasterVC
&c_SegueIdEmbedDetailVC
are constants with the corresponding ID of the segue IDs defined in the storyboard.If the nib is loaded it will call addChildViewController as part of the initialisation process
so a performant solution could be also to overwrite
there you can catch your childController e.g. by comparing its Class and assign it to a property / ivar
}
This will save your from iterating trough the childViewControllers.