I am a beginner, and I have a project in which I need to pass data back from a childviewcontroller. Specifically, I have a picker view in my container and I want to be able to use it to select an option (say to simplify just a color to chose from an array of ten colours). Then I would like to be able to access the chosen color in my parent view controller and do something with it. I have researched this for a couple of days now and the closest answer to what I am looking for I found in a related question on S.O. Here it is:
"Regarding passing value to the containerView controller, you can make a property in the ChildViewController of the value you want it to be passed. Then in your ParentViewController do something like the following:
self.ChildViewController.yourProperty = yourValue
The opposite can be done in 4 ways:
You can make a delegate protocol to communicate the data between your controllers.
You can post a notification in your ChildViewController and add the parent controller as an observer.
You can use KVO.
And the easiest way out, you can make a property in your parentviewController and access it like the following:"
((YourParentViewControllerClassType *)self.parentViewController).yourParentProperty = TheValueYouWant;
Now, I would like to try the fourth option first as delegation, kvo and so on are options I have read about but not ready to tackle yet. What I would need help with is the last option.
Say I had a property in my child view controller where I store the chosen color. Something like:
@interface ViewController ()
@property NSString *ChosenColorInChildVC;
@end
And then, later on:
self.ChosenColorInChildVC = [self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]];
how would I pass that value using the proposed:
((YourParentViewControllerClassType *)self.parentViewController).yourParentProperty = TheValueYouWant;
Can someone dumb it down a little further for me? Thanks
I'm going to explain you with an example how delegation works.
Edit your ChildViewController.h like this:
@protocol ChildViewControllerDelegate;
@interface ChildViewController : UIViewController
@property (weak)id <ChildViewControllerDelegate> delegate;
@end
@protocol ChildViewControllerDelegate <NSObject >
- (void) passValue:(UIColor *) theValue;
@end
On your ChildViewController.m when you want to pass something back to the ParentViewController , do like this:
- (void) myMethod
{
[delegate passValue:[UIColor redColor]]; // you pass the value here
}
On your ParentViewController.h
#import "ChildViewController.h"
@interface ParentViewController : UIViewController <ChildViewControllerDelegate > // you adopt the protocol
{
}
On your ParentViewController.m:
- (void) passValue:(UIColor *) theValue
{
//here you receive the color passed from ChildViewController
}
Now be careful. Everything will work only if you set the delegate. So when you declare ChildViewController inside your ParentViewController class, do like this:
ChildViewController * controller = [[ChildViewController alloc]initWithiNibName:@"ChildViewController"];
controller.delegate = self; //without this line the code won't work!