如何检测UIPickerView的变化?

问题描述 投票:24回答:3

我想检测UIPickerView值的变化。

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

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

} 

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

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

ios objective-c swift uipickerview
3个回答
56
投票

如果你看看它有UIPickerViewDelegate

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

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


6
投票

UIPickerViewDelegatepickerView:didSelectRow:inComponent:


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类中实时观察变化。


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
            }        
}
© www.soinside.com 2019 - 2024. All rights reserved.