【问题标题】:Two UI pickers in the same view controller Objective C?同一个视图控制器Objective C中的两个UI选择器?
【发布时间】:2016-07-16 18:17:20
【问题描述】:

所以我得到了我的 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]];
}

【问题讨论】:

  • 只需将tag 添加到故事板中的选择器视图中,然后在从数据源返回数据时,通过执行if picker.tag == 1 { ... } else if picker.tag == 2 { ... } 来检查哪个选择器正在请求数据

标签: ios uipicker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多