【问题标题】:View Picker update based on selection in other picker view根据其他选择器视图中的选择查看选择器更新
【发布时间】:2014-02-05 08:22:06
【问题描述】:

我有 2 个选择器,每个都有一个包含 4 个相同项目的数组。我希望当我在第一个选择器中选择任何值时,它会从第二个选择器中删除。

我写了一段代码,但它不起作用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.inWord.delegate = self;

    //Set tags to differentiate
    from.tag=1;
    to.tag=2;

    //Load  NSArray fromlang
    _fromlang= [[NSArray alloc] initWithObjects:@"English",@"Spanish",@"German",@"Chinese", nil];

    //Load  NSArray tolang
    _tolang= [[NSArray alloc] initWithObjects:@"1English",@"1Spanish",@"German",@"Chinese", nil];
}

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView.tag==1)        // from
    {
        return _fromlang.count;
    }
    else  //if(pickerView.tag==2)                      //to
    {
        return _tolang.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(pickerView.tag==1)          //from
    {
        if ([[_fromlang objectAtIndex:row ]  isEqual:@"Spanish"])
        {
            _tolang = @[ @"English",@"DeleteSanish",@"German",@"Chinese"];
        }
        return [_fromlang objectAtIndex:row];
    }
    else  //if(pickerView.tag==2)                    //to
    {
        return [_tolang objectAtIndex:row];
    }
}

我尝试过一种情况,如果我在第一个选择器中选择西班牙语,它会在第二个选择器中显示 deletespanish。但是默认情况下,我将第二个选择器设置为 deletespanish。

谁能告诉我发生了什么?

【问题讨论】:

    标签: ios cocoa-touch uipickerview


    【解决方案1】:

    它不起作用,因为在显示该特定行时会调用 pickerView:titleForRow:forComponent:。这发生在您的选择器的所有可见行上。

    选择发生在pickerView:didSelectRow:inComponent:。这就是您应该操作第二个选择器数组的内容的地方。在更新了支持选择器的数组后,您必须为第二个选择器调用 reloadAllComponentsreloadComponent:

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-21
      • 1970-01-01
      相关资源
      最近更新 更多