两个UI选择器在同一个视图控制器Objective C?

时间:2016-07-16 18:17:20

标签: ios uipicker

所以我得到了我的UI选择器,但它为每个UI选择器显示相同的数据,但我希望它在一个ui选择器中显示胡子数组,在另一个中显示颜色。当前在图像中显示它为每个阵列分配相同的数据。

- (void)viewDidLoad
{
    [super viewDidLoad];

    _colourSourceArray = [[NSArray alloc] initWithObjects:@"No Frame",@"Red", @"Green", @"Blue", @"Black",@"Yellow", nil ];
    _MustacheArray = [[NSArray alloc]initWithObjects:@"None",@"Pencil",@"The Professor",@"The Regent",@"Hipster",@"Super Mario", nil];

    [_picker selectRow:0 inComponent:0 animated:YES];
    [_notcolourpicker selectRow:1 inComponent:0 animated:YES];

    _picker.tag=0;
    _notcolourpicker.tag=1;
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return _colourSourceArray.count;
            break;
        case 1:
            return _MustacheArray.count;
        default:
            break;
    }

    return  0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [_colourSourceArray objectAtIndex:row];
            break;
        case 1:
            return [_MustacheArray objectAtIndex:row];
        default:
            break;
    }

    return  0;
}

-(IBAction)returnToExportSettingsVC:(UIStoryboardSegue *)segue
{
    // Nothing needed here.
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        NSLog(@"First");

        NSString *s = _colourSourceArray[row];

        _selectedcolour = s;
        NSLog(_selectedcolour);
    }
    else
        if(component == 1){
            NSLog(@"Second");
            NSString *d = _MustacheArray[row];

            _selectedmustache=d;

            NSLog(_selectedmustache);
        }

    /// Used if you wist to assign the selected row to a label to show a users selection.
    //_label.text=  [_MustacheArray objectAtIndex:[mostachepicker selectedRowInComponent:1]];
}

enter image description here

1 个答案:

答案 0 :(得分:3)

问题是两个选择器视图都调用相同的数据源/委托方法。如果您正在以这种方式构建代码,则需要检查pickerView参数以查看哪个选择器视图,然后启用它。