0

I'm trying to update the interface contents of a ContainerView on iOS (UIViewController embedded in a UIView) from the UIViewController that it's being displayed in. However, the ContainerView just won't update its content.

The ContainerView and the ViewController are associated with different classes. I can pass data between the two View Controllers by using a few methods like these:

- (void)displayStringInContainer:(NSString *)string

The string gets successfully passed to the ContainerView from the ViewController, however when I try to display that string in an interface element - nothing happens (even though the code is getting called):

self.buttonName.titleLabel.text = string;

I've even tried calling setNeedsDisplay on the button, but nothing happens. Note that this is happening with all interface items.

Here's how I call the method on the ContainerView from my ViewController:

ContainerViewController *cvc = [[ContainerViewController alloc] init];
[cvc displayStringInContainer:@"Text"];

I've done quite a bit of searching, but haven't found anything (also tried to look on the Apple Dev Site, but it's been down for the past three days :P). Does anyone know how to update the content of a ContainerViewController from another ViewController? Why isn't this working? I've been scratching my head on this for a while now.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
  • How do you had the container's view to your `UIViewController's` view? – Rui Peres Jul 20 '13 at 19:30
  • @JackyBoy The ContainerView is embedded (via storyboard segue) into a `UIView` inside of my `UIViewController`. I just dragged the Container View from the objects library onto my storyboard and hooked it up. – Sam Spencer Jul 20 '13 at 19:36
  • I don't use Storyboards, for a couple of reasons. You just gave me another one. Using code it would take you 30 seconds to do that. Sorry I can't help you. – Rui Peres Jul 20 '13 at 19:46
  • Seems like *cvc in this case is not the ContainerViewController in your storyboard, right? Shouldn't you be using the property you hooked up your Container View to? – Stakenborg Jul 20 '13 at 19:54
  • @Stakenborg Yes, you're correct. The Container View (in my storyboard) is a `UIView` with a ViewController embedded in it, therefore I cannot hook it up to the ContainerViewController (incompatible types here). – Sam Spencer Jul 20 '13 at 20:15

1 Answers1

1

Alloc init'ing cvc is not the right way to get your reference -- that's a new instance, not the same instance as the one embedded in your view. You can access that instance in code from the parent controller with self.childViewControllers[0] (assuming you have only one container view). You can also get the reference by implementing prepareForSegue and use segue.destinationController (that will be your embedded controller).

What you seem to be missing in your understanding, is that the controller you get when you use a container view in the storyboard is a child view controller. It's the same as if you had called [self addChildViewController:whatever] in code and then added the child's view as a subview of your view.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Thanks for the great explanation. I didn't know about Child View Controllers previously. Other than providing the Child VC's `index` (in this case 0) in the CVC array, is there any other way to access it? Maybe using the storyboard ID? – Sam Spencer Jul 21 '13 at 15:13
  • 1
    @RazorSharp, as I said, you can also use segue.destinationViewController from within prepareForSegue, but there's no way to access it by an identifier. Unfortunately, there's no viewControllerWithIdentifier: type method to just access a controller. You can only use its identifier to instantiate a new one, which you don't want to do. – rdelmar Jul 21 '13 at 16:02