Xcode storyboard Container View - how do I access a viewcontroller

I am trying to use a storyboard and work correctly. I added a Container View container for one of my existing views. When I try to add a link to this in my controller .h file (ctrl-drag), I get an IBOutlet UIView *containerView . How do I get a link to the container view controller? I need a container view controller, so I can configure it on my controller so that they can "talk" to each other.

I have a setup for my story:

enter image description here

And it refers to my .h file as:

enter image description here

Note on .h that there is a UIView, not my InstallViewController for the view. How to add a link to the view controller? I need to install a delegate.

+45
ios objective-c xcode uiviewcontroller container-view
Nov 07 '12 at 17:38
source share
5 answers

There is another solution by specifying the identifier for embedding segue (s) and retrieve the appropriate view controllers in the prepareForSegue: method prepareForSegue:

The advantage of this method is that you do not need to rely on the specific order in which the controllers of your child view are added, because each child view controller is embedded through a unique segue identifier.

Update 2013-01-17 - Example

 - (void) prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender { // -- Master View Controller if ([segue.identifier isEqualToString:c_SegueIdEmbedMasterVC]) { self.masterViewController = segue.destinationViewController; // ... } // -- Detail View Controller else if ([segue.identifier isEqualToString:c_SegueIdEmbedDetailVC]) { self.detailViewController = segue.destinationViewController; // ... } } 

c_SegueIdEmbedMasterVC and c_SegueIdEmbedDetailVC are constants with the corresponding identifier for the segment identifiers defined in the storyboard.

+67
Nov 16 '12 at 17:00
source share

When you add a container view, xcode calls the UIViewController addChildViewController: method addChildViewController:

In your case, you can get a ViewController container that looks for it in the SplashViewController childViewControllers list, something like this:

 for (UIViewController *childViewController in [self childViewControllers]) { if ([childViewController isKindOfClass:[InstallViewController class]]) { //found container view controller InstallViewController *installViewController = (InstallViewController *)childViewController; //do something with your container view viewcontroller break; } } 

I had the same doubt yesterday :)

+14
Nov 09
source share

Vitor Franchi's answer is correct, but can be more efficient and convenient. Especially when accessing the childhood viewing controller several times.

Create readonly property

 @interface MyViewController () @property (nonatomic, weak, readonly) InstallViewController *cachedInstallViewController; @end 

Then create a convenient getter method

 - (InstallViewController *)installViewController { if (_cachedInstallViewController) return _cachedInstallViewController; __block InstallViewController *blockInstallViewController = nil; NSArray *childViewControllers = self.childViewControllers; [childViewControllers enumerateObjectsUsingBlock:^(id childViewController, NSUInteger idx, BOOL *stop) { if ([childViewController isMemberOfClass:InstallViewController.class]) { blockInstallViewController = childViewController; *stop = YES; } }]; _cachedInstallViewController = blockInstallViewController; return _cachedInstallViewController; } 

Now access to the child view controller in this way

 [self.installViewController doSomething]; 
+2
Mar 31 '14 at 10:53
source share
 UIView* viewInsideOfContainer = installerView.subviews[0]; 

You will get a UIView inside a UIViewController that references your UIView. You can use subview for any type that inherits from UIView.

0
May 17 '13 at 17:37
source share

If nib loads, it will call addChildViewController as part of the initialization process

therefore, the execution solution may also be overwritten

 - (void)addChildViewController:(UIViewController *)childController 

there you can catch your childController, for example. comparing your class and assigning it to the / ivar property

 -(void)addChildViewController:(UIViewController *)childController { [super addChildViewController:childController]; if([childController isKindOfClass:[InstallViewController class]]) { self.installViewController = (InstallViewController *)childController; } 

}

This will save you from iterating through childViewControllers.

0
Mar 31 '14 at 11:35
source share



All Articles