2

I have a dilemma, I have two UIPickerViews which "show" when two distinct views load. I started with one UIPickerView and was able to get that up and running by loading the array and all the other UIPickerView actions within the ViewController.

I thought it would be as simple as copy/pasting the same methods for the new UIPickerView, but just changing the variable names, also within the same UIViewController.

Problem is - both UIPickerView are showing the same data set in the drop down ? Am I simply missing something obvious here?

Updated below

I did that for all four methods below and now that app crashes once the “next” view is loaded? Does this look right?

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 

- (NSInteger)thePickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component

- (NSString *)thePickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

- (void)thePickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
iHunter
  • 6,205
  • 3
  • 38
  • 56
timw07
  • 339
  • 2
  • 5
  • 18
  • yeah the methods look good, your crash is due to something else. Try the debugger! At Run->Debugger. Or post more code. – ThomasRS Apr 05 '11 at 01:36

1 Answers1

13

Yeah you are, you have to use the UIPickerView argument to determine WHICH picker view is actually asking for data - since you have set both pickers to the same data source.

-(void)numberOfComponentInPickerView:(UIPickerView*)thePickerView {
    if(thePickerView == firstPickerView) {
       // return number of components in first picker    
    } else if(thePickerView == secondPickerView) {    
       // return number of components in second picker    
    } else {
       assert(NO);
    }
}

// repeat for all other delegate / datasource methods
ThomasRS
  • 8,215
  • 5
  • 33
  • 48
  • Lol- yea, having a tough time with the formatting - give me one min! – timw07 Apr 05 '11 at 01:29
  • Updated, thanks I'm wondering if I need to initialize "thePickerView" ? If so - should I associate the UIPickerViews in IB to both "thePickerView" and "secondPickerView" for example? – timw07 Apr 05 '11 at 02:10
  • No, the thePickerView should be set - it is the picker view which is asking for data - no picker view, no asking for data. Set your picker views as iBOutlets and connect them in the xib. If you are having problems still, you can create two classes - one data source for each picker view, and manually assign them to the picker view instances in the viewDidLoad method. – ThomasRS Apr 05 '11 at 02:18