ios 如何检测 UIPickerView 上的变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2565805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect changes on UIPickerView?
提问by tristaf
I want to detect changes of UIPickerView value.
我想检测 UIPickerView 值的变化。
If UIPickerView respond to addTarget I used a code like this:
如果 UIPickerView 响应 addTarget 我使用了这样的代码:
-(void) valueChange:(id)sender { change = YES; } UIPickerView *questionPicker = [[UIPickerView alloc] init]; [questionPicker addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
How can I do same things but in a correct way ?
我怎样才能做到同样的事情,但以正确的方式?
回答by Kevin Sylvestre
If you look at the UIPickerViewDelegateit has:
如果你看看UIPickerViewDelegate它有:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
Simply set your picker views delegate and implement this.
只需设置您的选择器视图委托并实现它。
回答by gammelgul
UIPickerViewDelegate
has pickerView:didSelectRow:inComponent:
UIPickerViewDelegate
已 pickerView:didSelectRow:inComponent:
回答by tylerlantern
Better override this function
更好地覆盖此功能
public override func selectedRow(inComponent component: Int) -> Int {
let index = super.selectedRow(inComponent: component)
//call closure or delegate as you want
return index
}
in UIPickerViewclass to observe changing in realtime.
在UIPickerView类观察实时变化。
回答by Gian
Swift 4:Implement picker view delegates:
Swift 4:实现选择器视图委托:
optional public func pickerView(_ pickerView: UIPickerView, titleForRow row:
Int, forComponent component: Int) -> String?
Example:
例子:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,
forComponent component: Int) -> String? {
let value = dummyListArray[row]
if value == "someText"{
//Perform Action
}
return value
}
Or
或者
optional public func pickerView(_ pickerView: UIPickerView, didSelectRow row:
Int, inComponent component: Int)
Example:
例子:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,
inComponent component: Int) {
let value = dummyListArray[row]
if value == "someText"{
//Perform Action
}
}
回答by Haya Hashmat
You have to use picker view delegate method:
您必须使用选择器视图委托方法:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int,
inComponent component: Int) {
}