6

I have simple ViewController which have one container view with TableViewController. At ViewController I loading data from external API and one part of that data, I need to pass into TableViewController in container.

Is there any way how to do that? Thank you!

Martin
  • 490
  • 5
  • 16

1 Answers1

14

The controllers contained in a container view can be accessed by self.childViewControllers from the parent controller. If you only have one, then it will be at self.childViewControllers[0].


Note: regarding RD's excellent technique explained in the comment below; here's a typical example, tested and working: this simply goes in the VC of the overall scene. Just click on the segue itself (ie, the small symbol in the middle of the connecting arrow) to set the text identifier.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if ([segue.identifier isEqualToString:@"containerLogin"])
        self.vcLogin = (LoginVC *)segue.destinationViewController;

    if ([segue.identifier isEqualToString:@"containerStartNew"])
        self.vcStartNew = (StartNewVC *)segue.destinationViewController;

    }

Here's just how to do it in Swift: (you have to be careful with the unwrappers)

override func prepareForSegue(segue:(UIStoryboardSegue!), sender:AnyObject!)
    {
    if (segue.identifier == "feedContainer")
        {
        feed = segue!.destinationViewController as! Feed
        feed.someFunction()
        }
    }
Fattie
  • 27,874
  • 70
  • 431
  • 719
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • RD, what the hell to do if there's more than one? How do you know which is which?? Any ideas?? – Fattie May 11 '14 at 18:42
  • 2
    @JoeBlow, You can check the class of the controller you get back to determine which is which. You can also assign the controllers in prepareForSegue instead of doing it like I said in my answer (the embedded controller will be the segue.destinationViewController). That method is called for embedded controllers, so you can implement that, and check the segue's identifier to know which controller is which. That's probably the better way to do it, particularly if you have more than one child. – rdelmar May 11 '14 at 19:15
  • I tested it and that works perfectly. Brilliant. Amazing that is not more commonly known. – Fattie May 11 '14 at 19:32
  • I asked a question about the array order on the "other" method .. http://stackoverflow.com/questions/23597123/in-fact-is-destinationviewcontroller-in-storyboard-order – Fattie May 11 '14 at 19:45
  • @rdelmar I know this is an old question, but I'll try. How can I do the opposite? Meaning, how can I access the `viewController` from the `container view`? – Horray May 04 '15 at 20:54
  • @Horray If you mean from the view controller embedded in the container view, you can use self.parentViewController. – rdelmar May 05 '15 at 00:16
  • @Horray Where did log it? I think it's still nil in viewDidLoad. – rdelmar May 05 '15 at 00:35
  • I assigned it like this: self.theMainViewController = (theMainViewController *)self.parentViewController; then in another method (already called) I nsloged it, and it gave me a nil – Horray May 05 '15 at 00:41
  • @Horray In what method did you do the assignment? Did you do that in viewDidLoad? – rdelmar May 05 '15 at 01:37
  • I assigned it in viewDidLoad – Horray May 05 '15 at 01:41
  • @Horray As I said, that's too early. Try it in viewDidAppear. – rdelmar May 05 '15 at 03:12
  • Oh!! My bad! I thought you meant I shouldn't call it in `viewDidLoad`. Sorry. Thanks!! – Horray May 05 '15 at 04:28
  • This answer is amazing. Much cleaner than iterating the parent's child view controllers. – John Rogers Feb 01 '16 at 03:09