I'm trying to have 2 pickerviews in the same view. It works except for two things. If one pickerview has more rows than the other the app crashes when picking an item from the pickerview with more items. Also I created an NSLog for the pickerviews and the console shows that I'm picking two items at once when in fact i'm only dealing with one pickerview. I know it sounds a bit confusing but I'm including all the code. Thank you in advance.
list and list2 are NSMutableArrays
list has 4 items list2 has 5 items
There error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (4) beyond bounds (4)'
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
if([thePickerView isEqual:pickerView1 ]){
return 1;
}
else if([thePickerView isEqual:pickerView2]){
return 1;
}
else{
return 0;
}
}
-(NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component{
if([thePickerView isEqual:pickerView1 ]){
return [list count];
}
else if([thePickerView isEqual:pickerView2]){
return [list2 count];
}
else{
return 0;
}
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if([thePickerView isEqual:pickerView1 ]){
return [list objectAtIndex:row];
}
else if([thePickerView isEqual:pickerView2]){
return [list2 objectAtIndex:row];
}
else{
return 0;
}
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);
if([thePickerView isEqual:pickerView1 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
//Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
//Do Something
}
}