菜鸟教程小白 发表于 2022-12-13 09:52:49

ios - 基于另一个 UIPickerView 显示 UIPickerView


                                            <p><p>我有两个不同的 <code>UIPickerView</code> 用户选择第一个 <code>UIPickerView</code> 并从第一个选择器的 <code>didSelectRow</code> 中,我得到第二个的输入 <code>UIPickerView</code>。我尝试为单个选择器设置标签,但没有任何效果。我的代码如下。</p>

<pre><code>//Picker-1
pickerView1 = [ init];
pickerView1 setDataSource: self];
;
;
pickerView1.showsSelectionIndicator = YES;
;

//Picker-2
pickerView2 = [ init];
pickerView2 setDataSource: self];
;
;
pickerView2.showsSelectionIndicator = YES;
;

//Picker -1 rows
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return getvalue1.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
      return ;
    }
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
      NSLog(@&#34;You selected this: %@&#34;, );
      NSArray *getvalue2 =;

    }

//Picker-2 rows

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

-(NSInteger)pickerView2:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return getvalue2.count;
}

-(NSString *)pickerView2:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{   
    return ;
}

-(void)pickerView2:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@&#34;You selected this: %@&#34;, );
}
</code></pre>

<p>问题是在显示时都获得了第一个选择器值。选择器 2 行没有被调用。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您只能拥有一组选取器 View 委托(delegate)方法。使用每个的 <code>pickerView</code> 参数来做正确的事情:</p>

<pre><code>- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
      return getvalue1.count;
    } else {
      return getValue2.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
      return ;
    } else {
      return ;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (pickerView == pickerView1) {
      NSLog(@&#34;You selected this: %@&#34;, );
      NSArray *getvalue2 =;
    } else {
      NSLog(@&#34;You selected this: %@&#34;, );
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 基于另一个 UIPickerView 显示 UIPickerView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/32061357/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/32061357/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 基于另一个 UIPickerView 显示 UIPickerView