Access Container View Controller from Parent iOS

in iOS6 I noticed the new Container View but am not quite sure how to access it's controller from the containing view.

Scenario:

例

I want to access the labels in Alert view controller from the view controller that houses the container view.

There's a segue between them, can I use that?


Yes, you can use the segue to get access the child view controller (and its view and subviews). Give the segue an identifier (such as alertview_embed ), using the Attributes inspector in Storyboard. Then have the parent view controller (the one housing the container view) implement a method like this:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   NSString * segueName = segue.identifier;
   if ([segueName isEqualToString: @"alertview_embed"]) {
       AlertViewController * childViewController = (AlertViewController *) [segue destinationViewController];
       AlertView * alertView = childViewController.view;
       // do something with the AlertView's subviews here...
   }
}

你可以简单地用self.childViewControllers.lastObject (假设你只有一个孩子,否则使用objectAtIndex: self.childViewControllers.lastObject


for Swift Programming

you can write like this

var containerViewController: ExampleViewController?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // you can set this name in 'segue.embed' in storyboard
    if segue.identifier == "checkinPopupIdentifierInStoryBoard" {
        let connectContainerViewController = segue.destinationViewController as ExampleViewController
        containerViewController = connectContainerViewController
    }
}
链接地址: http://www.djcxy.com/p/87692.html

上一篇: 如何在UIView中加载xib文件

下一篇: 从父iOS访问容器视图控制器