1

Why isn't this storing the pickerView selection into the property of my mainMatrix object?

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [mainMatrix setM:[[self.numbers objectAtIndex:[pickerView selectedRowInComponent:0]] intValue]];
    [mainMatrix setN:[[self.numbers objectAtIndex:[pickerView selectedRowInComponent:1]] intValue]];
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alex
  • 451
  • 5
  • 13
  • 1
    check that `[self.numbers objectAtIndex:[pickerView selectedRowInComponent:0]]` is actually returning the proper value – jere Oct 30 '12 at 18:35
  • u need to use this code on action of any done button for ur selected values from pickerView... – vishy Oct 30 '12 at 18:35

2 Answers2

4

Split your code up so you can actually see what is going on:

NSUInteger selectedRow = [pickerView selectedRowInComponent:0];
int num = [self.numbers[selectedRow] intValue];
[mainMatrix setM:num];

selectedRow = [pickerView selectedRowInComponent:1];
int num = [self.numbers[selectedRow] intValue];
[mainMatrix setN:num];

Now you can debug the code and see what is actually happening. Readable, debuggable code is much better than cramming it all onto one line.

Make sure that mainMatrix is non-nil at this point in the code. Also make sure self.numbers is non-nil. And last (really first), make sure you have set the delegate on the picker view.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

Do you have a same array to populate both components in pickerview ?? if yes then try this code.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0) {
          [mainMatrix setM:[[self.numbers objectAtIndex:row] intValue]];
      }else if(component == 1){
          [mainMatrix setN:[[self.numbers objectAtIndex:row] intValue]];
      }
}
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81