0

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

Paul
  • 1,277
  • 5
  • 28
  • 56

1 Answers1

0

It sounds like your delegation advice is good, but that it left off a parameter for building the view controller via a storyboard.

ChildViewController *controller = [[ChildViewController alloc]
        initWithNibName:@"ChildViewController"
                 bundle:[NSBundle mainBundle]];

...will work, but only if there's a interface builder file in the project called "ChildViewController".

danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks, I have been thinking about this all day. I have tried what you suggest, still no love. I have actually made yet a simpler project, if you definitely don't have something more interesting to do, I'd appreciate it if you had a quick look. I will understand delegation if it's the last thing I do! http://stackoverflow.com/questions/34983566/delegation-and-passing-data-back-from-childviewcontroller – Paul Jan 25 '16 at 00:59