从第二个选择器列中选择值

时间:2014-09-06 17:23:55

标签: ios uipickerview uipickerviewcontroller uipickerviewdatasource

我正在使用一个uipicker应用程序,其中我有两列选定数据,使用此代码我可以从选择器中选择值(当它只有一行时)

- (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

使用此代码,当我点击我的按钮时,我有所选内容的值,然后我可以继续推送我的segue或我喜欢的任何内容。

但是,我希望mypicker有两行,但是下面发布了代码...当我点击按钮时,我不能收到错误。

- (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

数据显示正常,但我不知道如何在单击我的按钮时选择来自我的两行的值。 任何有关这方面的帮助将不胜感激。

谢谢

0 个答案:

没有答案