我想检测UIPickerView值的变化。
如果UIPickerView响应addTarget,我使用了这样的代码:
-(void) valueChange:(id)sender { change = YES; } UIPickerView *questionPicker = [[UIPickerView alloc] init]; [questionPicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
我怎样才能以正确的方式做同样的事情?
答案 0 :(得分:56)
如果您查看它有UIPickerViewDelegate:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
只需设置您的选择器视图委托并实现此目的。
答案 1 :(得分:6)
UIPickerViewDelegate
有pickerView:didSelectRow:inComponent:
答案 2 :(得分: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 类中实时观察变化。
答案 3 :(得分:0)
快速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
}
}
答案 4 :(得分:0)
您必须使用选择器视图委托方法:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,
inComponent component: Int) {
}