Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass data from my ViewController to a Container View?

Tags:

I have a storyboard set up in XCode and have a MainViewController. In the MainViewController I have added a ContainerView which naturally creates a Segue with another VIewController.

In my MainViewController.m file I have set up data and want to link this data to a label in the ContainerView however I thought I could click on the File's Owner and do this but of course I can't because they are 2 different viewcontrollers now.

Can someone please help me because I'm struggling with this. There must be an easy way but I can't crack it!

Thank you

like image 236
Omar Avatar asked Jul 20 '13 14:07

Omar


People also ask

What is the difference between View and ViewController?

Views are dumb, and do not know about the structure of your application, and are simply told to display themselves in some state. A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews.


1 Answers

You can use prepareForSegue just like any other two controllers -- that method will be called after the two controllers are instantiated, but before either viewDidLoad runs. The other way to do this is to use the parent controller's childViewControllers property (the embedded controller is a child). So, the child will be self.childViewControllers[0].

After Edit:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {     if ([[segue identifier] isEqualToString:@"EmbedSegue"]) {         MyEmbeddedController *embed = segue.destinationViewController;         embed.labelString = self.stringToPass;     } } 

Of course, you have to change the names to what you have. Make sure the name you give to the segue in IB matches the one you check for in the if statement. In this example labelString is a string property you set up in your embedded controller. Then in that controller's viewDidLoad method, you can set the value of the label with that string.

like image 183
rdelmar Avatar answered Oct 13 '22 21:10

rdelmar