0

With the storyboard I've built one ViewController1 with an "Add" button. Click on add display a PopoverView (in ViewController2) with only a Picker within.

So my PickerView's delegate is in ViewController 2 but I want it in ViewController1... Is there a easy way to do this so ?

Thanks

Benjamin
  • 7,055
  • 6
  • 40
  • 60

2 Answers2

1

In your ViewController1 prepareForSegue method, try this:

if ([segue.identifier isEqualToString:@"MySegue"])
{
    ViewController2* vc2 = segue.destinationViewController;
    vc2.pickerView.delegate = self;
}

It basically assigns the delegate of the pickerview to ViewController1 before the segue is finished being called.

This of course is assuming your pickerview is setup as a property.

valheru
  • 2,552
  • 3
  • 20
  • 40
0

In your ViewController1.h file, change your interface header to this;

@interface ViewController1 : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>;

That makes your ViewController1 in charge of the delegates. Then add the methods you want in the ViewController1.m file. Also, set the delegate of the UIPickerView to your instance of ViewController1 with this:

[yourPickerview setDelegate:ViewController1];
Martol1ni
  • 4,684
  • 2
  • 29
  • 39
  • How this can help me to have delegate in ViewController 1 ? By doing so, I'll have delegate in ViewController 2 instead of ViewController 1, no ? UPDATE: I juste want my delegate in ViewC1 instead of ViewC2 – Benjamin May 05 '12 at 21:30
  • read my edit, otherwise tell me more about what's wrong and what you've done. Put up some code :) – Martol1ni May 05 '12 at 21:37
  • Img gonna try, come back in few minutes – Benjamin May 05 '12 at 21:38
  • Not working... In ViewController1.h: `@interface ViewController : UIViewController` In ViewController1.m: ` - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1;} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return ([categoryName count])-1;} - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [categoryName objectAtIndex:row]; }` – Benjamin May 05 '12 at 21:48
  • In ViewController2.h: `@interface PopOverViewController : UIViewController{` ` __weak IBOutlet UIPickerView *categoryPicker;` `}` In ViewController2.m: `- (void)viewDidLoad` `{` `ViewController *vc;` `[categoryPicker setDelegate:vc];` `[super viewDidLoad];` `}` – Benjamin May 05 '12 at 21:51
  • Sorry for indent but I can't reply as answer to my own question because I've not enough reputation. – Benjamin May 05 '12 at 21:52
  • Error message: 2012-05-05 23:40:48.327 Interface[302:f803] -[PopOverViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x6c3ae30 2012-05-05 23:40:48.343 Interface[302:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PopOverViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x6c3ae30' – Benjamin May 05 '12 at 21:53
  • Ok, I've fixed my issue. I've forgotten to init ViewController. – Benjamin May 05 '12 at 21:59
  • Good to hear. Next time, just edit your post, it's easier to read. And dont forget to set the question to solved. – Martol1ni May 05 '12 at 22:28