views:

218

answers:

1

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
}
}
+1  A: 

Your problem is here:

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);

What happens is that one of the above lists is sent a message to return an object which is out of its bounds. You should check what picker the call came from before sending message to the log:

if([thePickerView isEqual:pickerView1 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView2 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list2 objectAtIndex:row], row);
  //Do Something
}
else if([thePickerView isEqual:pickerView3 ]){
  NSLog(@"Selected item %@. Index of selected item:%i", [list3 objectAtIndex:row], row);
  //Do Something
}
pheelicks
Thanks so much!
NextRev
I'd recommend you'd learn to use the Xcode debugger better. When you get this kind of error the debugger will help you track down exactly what line of code your program failed on. From there it is much easier to spot what has gone wrong
pheelicks