2

我有两个 UIPickerViews 从数组中提取数据,尽管我似乎无法单独对它们进行编程。这是我用于 UIPickerViews 的代码:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [treatments count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
      return[[treatments objectAtIndex:row]valueForKey:@"treatmentName"];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _buttonText.text = [[treatments objectAtIndex:row]valueForKey:@"treatmentName"];
    if (![_buttonText.text isEqual: @"Pick a Treatment Name"]) {
        _buttonText.textColor = [UIColor blackColor];
    }
}

-(NSInteger)nursePicker:(UIPickerView *)nursePicker numberOfRowsInComponent:(NSInteger)component {
    return [nurses count];
}

- (NSString *)nursePicker:(UIPickerView *)nursePicker titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return[[nurses objectAtIndex:row]valueForKey:@"nurseName"];
}

当我运行代码时,选择器显示相同的数据

提前致谢

4

2 回答 2

7

存储对您的两个选择器视图的引用并使用作为参数传递给数据源方法的 UIPickerView 以确定您正在使用哪个选择器视图。

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == self.nursePicker) {
        return [nurses count];
    }
    else if (pickerView == self.treatmentPicker) {
        return [treatments count];
    }
}

每个数据源方法的想法相同

于 2013-07-27T15:40:18.407 回答
0

你应该放一个 BOOL 来检查数据。创建一个名为处理的 BOOL,当第一个选择器可用时,将 BOOL 设置为 YES,当第二个选择器可用时,将 BOOL 设置为 NO。然后检查您的方法以查看哪个选择器是他们的并放入数据。

@interface NotesViewController ()
{
    BOOL treatment; 
}

- (void)firstPickerComesUP
{
    treatment = YES;
} 

- (void)secondPickerComesUP
{
    treatment = NO;
} 
于 2013-07-27T15:37:23.970 回答