4

I have a UIPickerView on a UIView. I've implemented its protocol in the .h and delegates in the .m files:

 <UIPickerViewDataSource, UIPickerViewDelegate>

In IB, I've connected the above to the picker, which I also have an IBoutlet for. The methods look like this:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [self.arr count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row  forComponent:(NSInteger)component {
return @"test";
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//do something
}

Any ideas which piece I'm missing to get the picker working?

4thSpace
  • 43,672
  • 97
  • 296
  • 475

3 Answers3

7

If the delegate methods are not getting called, then in IB double check that the picker view's delegate and datasource outlets are connected to File's Owner.

In the .h file, the picker view outlet should be declared like this:

@property (nonatomic, retain) IBOutlet UIPickerView *pickerView;

  • All of those were fine. I discovered that for some reason the UIPickerView wasn't being initialized. I disconnected its IBoutlet and reconnected. All is fine now. – 4thSpace May 02 '10 at 20:33
  • this didn't do anything for me. I just dragged from storyboard and Xcode made that property a bit differently. @property (weak, nonatomic) IBOutlet UIPickerView *departmentPickerView; – coolcool1994 Apr 20 '13 at 22:27
3

you have not assigned the delegate to the UIPickerView thats the reason none of the delegate methods are called.

 UIPickerView *pickerView;
 pickerView.delegate = self;//write this line in view did load method.

Hope this helps and let me know if this answers your question..:)

Dalee Davis
  • 981
  • 8
  • 19
1

sometimes it's due to the pickerview not being connected to the file Owner's datasource and delegate in the nib

DanKiiing
  • 102
  • 1
  • 11