0

I am using a uipicker app where I have two columns of selected data, using this code I can select the values fromm the picker (when it only has one row)

- (void)viewDidLoad
{
    [super viewDidLoad];
_pickerData = @[@"item1", @"item2", @"Item 4", @"Item 5", @"Item 6"];
    self.picker.dataSource = self;
    self.picker.delegate = self;

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}


// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
     return 1;
  //  return 2;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;

}

- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return _pickerData[row];
  //    return _pickerData[row][component];
}


- (IBAction)selOptions:(id)sender {
    NSInteger row = [self.picker selectedRowInComponent:0];
    NSString *selected = [_pickerData objectAtIndex:row];

    NSLog(selected);

}
@end

With this code when I click my button I have the value of whatwas selected and then I can go on to push my segue or whatever I like.

However, I would like mypicker to have two rows, but with the code posted below...I cannot I get an error when I click the button.

- (void)viewDidLoad
{
    [super viewDidLoad];

     _pickerData = @[ @[@"item1", @"A"],
                     @[@"item2", @"B"],
                     @[@"item3", @"C"],
                     @[@"item4", @"D"],
                     @[@"item5",@"E"],
                     @[@"item6",@"F"]
                     ];

    // Connect data
    self.picker.dataSource = self;
    self.picker.delegate = self;

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// The number of columns of data
- (int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
   //  return 1;
     return 2;
}

// The number of rows of data
- (int)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;

}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

     return _pickerData[row][component];
}


- (IBAction)selOptions:(id)sender {
    NSInteger row = [self.picker selectedRowInComponent:0];
    NSString *selected = [_pickerData objectAtIndex:row];

    NSLog(selected);

NSInteger row2=[self.picker selectedRowInComponent:1];
NSString *selectedb=[_pickerData objectAtIndex:row2];

NSLog(selectedb);

}
@end

The data is displayed fine but, I do not know how to allow for values from both of my rows to be selected upon clicking my button. Any help on this would be appreciated.

Thank you

Sleep Paralysis
  • 449
  • 7
  • 22
  • 1
    [self.picker selectedRowInComponent:0]; and [self.picker selectedRowInComponent:1]; – iphonic Sep 06 '14 at 17:44
  • @iphonic I added the update to my orginal question. however i stil get the error.unrecognizeed selector sent to instance – Sleep Paralysis Sep 06 '14 at 17:53
  • I guess the error is because of `NSLog(selected);` and `NSLog(selectedb);` please change this to `NSLog(@"%@",selected);` and `NSLog(@"%@",selectedb);` – iphonic Sep 06 '14 at 18:00
  • oh @iphonic I npormally get these warnings but ignore them cause they never gave me an error before. Thanks, that solved it – Sleep Paralysis Sep 06 '14 at 18:04
  • @iphonic is there any reason why if ([selectedb isEqualToString:@"Test"]) { NSLog(@"Test"); } should crash the program saying NSarrayI isEqualToString unrecognized selector sent to instance? cause as far as i know (and correct me if im wrong) selected is no longer a string but has the single value from the array saved to it – Sleep Paralysis Sep 06 '14 at 18:14
  • i get back ( item1, "a" ) from my NSLog – Sleep Paralysis Sep 06 '14 at 18:18
  • Yes it will crash as `isEqualToString` is `NSString` method, it will not work with `NSArray`. – iphonic Sep 06 '14 at 18:24
  • @iphonic I was under the assumption that NSString *selected = [_pickerData objectAtIndex:row]; would only assign one word to my variable but apprantly its doing 2. So i will just use Row in my variable – Sleep Paralysis Sep 06 '14 at 18:27

0 Answers0