Search code examples
iosuistoryboardseguecontainer-view

ChildViewController -> ParentViewController -> SecondChildViewController using ContainerView


I have a ViewController that houses 2 Containers (one top, one bottom). Top Container houses a ViewController with a button and a textfield (just for illustrating purposes). What I want to do is:

  1. Type text
  2. Press button
  3. Send text to bottomViewController
  4. Display into table

How do I send text to the bottomViewController? Do I somehow have to talk to the parent?

I'm using Storyboards so I have this in the parentViewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"Current Segue: %@", segue.identifier);
    if ([segue.identifier isEqualToString:@"topSegue"]) {
        self.enterCommentViewController = segue.destinationViewController;
    }
    else if ([segue.identifier isEqualToString:@"bottomSegue"]) {
        self.commentsViewController = segue.destinationViewController;
    }
}

Solution

  • You don't use embed segues like that. The 2 controllers are instantiated at the same time as the parent controller. You can get a reference to these 2 child controllers from the parent controller's childViewControllers property. You should log childViewControllers to see which member of the array is which controller, and then you can just refer to those controllers as self.childViewControllers[0], and self.childViewControllers[1].