Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

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

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.

Upvotes: 0

Views: 5265

Answers (4)

Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

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.

Upvotes: 0

Mike
Mike

Reputation: 11

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;

Upvotes: 1

Suresh Varma
Suresh Varma

Reputation: 9740

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...

Upvotes: 0

Daniel Dickison
Daniel Dickison

Reputation: 21882

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.

Upvotes: 4

Related Questions