From within a view controller in a container view, how do you access the view controller containing the container? From within a view controller in a container view, how do you access the view controller containing the container? ios ios

From within a view controller in a container view, how do you access the view controller containing the container?


You can use the prepareForSeguemethod in Vc1 as an embed segue occurs when the ContainerViewController is made a child. you can pass self as an obj or store a reference to the child for later use.

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    NSString * segueName = segue.identifier;    if ([segueName isEqualToString: @"embedseg"]) {        UINavigationController * navViewController = (UINavigationController *) [segue destinationViewController];        Vc2 *detail=[navViewController viewControllers][0];        Vc2.parentController=self;    }}

Edit: minor code fix


To access parent view controller from within your child view controller you must override didMoveToParentViewController:

- (void)didMoveToParentViewController:(UIViewController *)parent {    [super didMoveToParentViewController:parent];    //Use parent}

On Xcode Command+Click over this method for more info:

These two methods are public for container subclasses to call when transitioning between child controllers. If they are overridden, the overrides should ensure to call the super. The parent argument in both of these methods is nil when a child is being removed from its parent; otherwise it is equal to the new parent view controller.

addChildViewController: will call [child willMoveToParentViewController:self] before adding the child. However, it will not call didMoveToParentViewController:. It is expected that a container view controller subclass will make this call after a transition to the new child has completed or, in the case of no transition, immediately after the call to addChildViewController:. Similarly removeFromParentViewController: does not call [self willMoveToParentViewController:nil] before removing the child. This is also the responsibilty of the container subclass. Container subclasses will typically define a method that transitions to a new child by first calling addChildViewController:, then executing a transition which will add the new child's view into the view hierarchy of its parent, and finally will call didMoveToParentViewController:. Similarly, subclasses will typically define a method that removes a child in the reverse manner by first calling [child willMoveToParentViewController:nil].


You can use delegation using the same method Bonnie used. Here is how you do it:

In your containerViews ViewController:

class ContainerViewViewController: UIViewController {   //viewDidLoad and other methods   var delegate: ContainerViewControllerProtocol?   @IBAction func someButtonTouched(sender: AnyObject) {     self.delegate?.someDelegateMethod() //call this anywhere   }}protocol ContainerViewControllerProtocol {    func someDelegateMethod()}

In your parent ViewController:

class ParentViewController: UIViewController, ContainerViewControllerProtocol {   //viewDidLoad and other methods    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {        if segue.identifier == "filterEmbedSegue" {            let containerViewViewController = segue.destinationViewController as ContainerViewViewController            containerViewViewController.delegate = self        }    }    func someDelegateMethod() {        //do your thing    }}