我想检测UIPickerView值的变化。
如果UIPickerView响应addTarget我使用了这样的代码:
-(void) valueChange:(id)sender { change = YES; } UIPickerView *questionPicker = [[UIPickerView alloc] init]; [questionPicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
我怎么能以正确的方式做同样的事情呢?
如果你看看它有UIPickerViewDelegate:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
只需设置您的选择器视图委托并实现它。
UIPickerViewDelegate
有pickerView:didSelectRow:inComponent:
更好地覆盖此功能
public override func selectedRow(inComponent component: Int) -> Int {
let index = super.selectedRow(inComponent: component)
//call closure or delegate as you want
return index
}
在UIPickerView类中实时观察变化。
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
}
}