Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top