Selecting a row in a UIPickerView dynamically
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 va开发者_C百科lue 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.
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.
Also make sure that picker is defined with "IBOutlet" as follows. And connect the picker's referencing outlet to this:
@property (nonatomic, retain) IBOutlet UIPickerView *picker;
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...
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.
精彩评论