this is a question regarding delegation, trying to pass some data back from a child view controller that is linked to a container view which has a picker view. I asked a related question before and someone was kind enough to reply with the following code:
"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!"
Anyway, this helped me quite a bit and is one of the most helpful pieces of advice I have got yet. Unfortunately, one of the lines in this code returns an error and is making the delegate nil. It is this one:
ChildViewController * controller = [[ChildViewController alloc]initWithiNibName:@"ChildViewController"];
the error is: "No visible @interface for 'ChildViewController' declares the selector 'initWithNibName:'
As I am following instructions on something I am trying to learn, I am a bit lost. To begin with, when I try to type it, the proposed method takes another argument: [[ChildViewController alloc]initWithNibName:<#(nullable NSString *)#> bundle:<#(nullable NSBundle *)#>];
I have done a NSLog to get the [self.delegate description] and it's come back null. Can anyone help? thanks