31

我想检测 UIPickerView 值的变化。

如果 UIPickerView 响应 addTarget 我使用了这样的代码:

-(void) valueChange:(id)sender {
    change = YES;
} 

UIPickerView *questionPicker = [[UIPickerView alloc] init]; 
[questionPicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

我怎样才能以正确的方式做同样的事情?

4

6 回答 6

61

如果您查看UIPickerViewDelegate它有:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

只需设置您的选择器视图委托并实现它。

于 2010-04-02T08:57:48.370 回答
6

UIPickerViewDelegatepickerView:didSelectRow:inComponent:

于 2010-04-02T08:58:29.933 回答
1

更好地覆盖这个函数

public override func selectedRow(inComponent component: Int) -> Int {
  let index = super.selectedRow(inComponent: component)
  //call closure or delegate as you want
  return index
}

UIPickerView类中实时观察变化。

于 2019-03-07T08:34:08.733 回答
1

Swift 4:实现选择器视图委托:

optional public func pickerView(_ pickerView: UIPickerView, titleForRow row: 
   Int, forComponent component: Int) -> String?

例子:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, 
forComponent component: Int) -> String? {
            let value = dummyListArray[row]
            if value == "someText"{
              //Perform Action
            }
            return value
}

或者

optional public func pickerView(_ pickerView: UIPickerView, didSelectRow row: 
   Int, inComponent component: Int)

例子:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, 
inComponent component: Int) {
            let value = dummyListArray[row]
            if value == "someText"{
              //Perform Action
            }        
}
于 2019-05-24T05:55:22.927 回答
0
func pickerView(UIPickerView, didSelectRow: Int, inComponent: Int)

当用户选择组件中的一行时,由选取器视图调用。

例如,这允许在选择更改时更新文本字段,只需松开手指即可。

于 2021-01-15T03:12:55.120 回答
0

您必须使用选取器视图委托方法:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
于 2020-02-06T09:51:29.777 回答