Benjamin
Benjamin

Reputation: 7368

Delegate PickerView's delegate to another view

First of all, I would like to say that I'm just beginning "learning" objective-c. Sorry if I've done big mistake

Im trying to delegate -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component from view 2 to view 1...

View 1.h:

@interface ViewController : UIViewController <PopOverViewControllerDelegate>

View 1.m:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component{ //stuff here}

View 2.h:

@protocol PopOverViewControllerDelegate <NSObject, UIPickerViewDelegate>
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component;
@end

@interface PopOverViewController : UIViewController <UIPickerViewDataSource>

View 2.m:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component{

[self.delegate pickerView:categoryPicker didSelectRow:row inComponent:component];

}

It does not work...

Upvotes: 1

Views: 547

Answers (2)

saadnib
saadnib

Reputation: 11145

To pass the delegate to a different class you just need to set the delegate of UIPickerView to your another class. In your class 2 define delegate methods that you want to handle and in .h of class 2 confirm UIPicklerView delegate. Then in class 1 you just need to write this line -

[pickerView setDelegate:class2];

in the above line pickerView is the reference of UIPickerView and class2 is the reference to the instance to class2.

Also please have a look at @Caleb answer. i am completely agree with him . By doing above thing you are making things more complex for you.

Upvotes: 0

Caleb
Caleb

Reputation: 124997

  1. It's a little strange for a view to be a delegate of another view. Views really shouldn't know much about data -- it's the controller that usually makes the kinds of decisions that are required of a delegate.

  2. Instead of having one delegate (View1) pass the picker's delegate messages on to another delegate (View2), why not just make View2 the picker's delegate? It seems like you're making things more complicated than they need to be.

  3. Please tell us more about the problem than "it does not work." If it worked, you wouldn't be asking in the first place. Tell us how it doesn't work. Does the method in View2 ever get called? If yes, then what's the problem? If no, why not? What happens in View1 that causes it not to call View2?

Upvotes: 1

Related Questions