views:

99

answers:

3

Hello everyone ^.^,

I have an iPhone app that use UIPickerView and 3 buttons.
This picker has one component with 100 rows (0, 1, 2, ..., 98, 99).

I'd like to implement the code so that, when I press on the buttons

  • button1: will make the picker select row 21th (which has value 20)
  • button2: will make the picker select row 41th (which has value 40)
  • button3: will make the picker select row 60th (which has value 60)

I know how to make the picker select a row when the view first load by put the following code in to viewDidLoad() method:

(void)viewDidLoad
{
........
[picker selectRow:50 inComponent:0 animated:YES];
}
but I could not make the picker select the desired row when I tap the buttons.

Any help would be appreciated!

Thanks for reading ^.^

+3  A: 

Hook the button to an action method, then in your action method simply call the same selectRow:inComponent:animated: method, i.e.

- (IBAction)button1Action {
    [picker selectRow:20 inComponent:0 animated:YES];
}

And you hook up the button to that method in Interface Builder.

Daniel Dickison
Yes, I did that. I even call 'reloadComponent' from the picker but it still does not work. I must be missing something here. This is the code I use for 'button1' in my app. - (IBAction) goToRowTwenty { [picker selectRow:20 inComponent:0 animated:YES]; [timerPickerView reloadComponent:0]; }
Tuyen Nguyen
A: 

you can put the same code that you have kept in viewDidLoad for selecting pickerRow on the event that get fires when button is clicked.

-(void)button1clickevent{
//FUNCTION MUST GET FIRE WHEN BUttON ! IS CLICKED
       [picker selectRow:50 inComponent:0 animated:YES];
}

HAPPY iCODING...

Suriya
Yes, I did that. I even call 'reloadComponent' from the picker but it still does not work. I must be missing something here. This is the code I use for 'button1' in my app. - (IBAction) goToRowTwenty { [picker selectRow:20 inComponent:0 animated:YES]; [timerPickerView reloadComponent:0]; }
Tuyen Nguyen
and whats this timepickerView here... reload your picker [picker reloadComponent:0] not [timepicker reloadComponent:0]; Also theres no need to reload picker. Please check out whether your picker is really binded with the picker in xib.
Suriya
A: 

OK, now I recreate the project and retype the code:
- (IBAction) goToRowTwenty
{
[picker selectRow:20 inComponent:0 animated:YES];
[timerPickerView reloadComponent:0];
}
and it works, I really don't know what happened,
but thank you guys for answering my question. I appreciate your helps.

Tuyen Nguyen