Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect changes on UIPickerView?

I want to detect changes of UIPickerView value.

If UIPickerView respond to addTarget I used a code like this:

-(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 ?

like image 717
tristaf Avatar asked Apr 02 '10 08:04

tristaf


3 Answers

If you look at the UIPickerViewDelegate it has:

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

Simply set your picker views delegate and implement this.

like image 114
Kevin Sylvestre Avatar answered Nov 13 '22 02:11

Kevin Sylvestre


UIPickerViewDelegate has pickerView:didSelectRow:inComponent:

like image 6
gammelgul Avatar answered Nov 13 '22 02:11

gammelgul


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 UIPickerView class to observe changing in realtime.

like image 1
tylerlantern Avatar answered Nov 13 '22 00:11

tylerlantern