Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPickerView select first row programmatically

There are a lot of questions like this, but I believe my case is unique.

I have a pickerview that pops up when a textbox is clicked inside. The user selects their location in the pickerview, and it's put into the textbox.

When opening the pickerview, the slider is on the first element, but if i click the done button to minimize the pickerview, that no element is selected (i.e. you must scroll down and back up to select the first element)

I've tried

    [pickerView selectRow:0 inComponent:0 animated:YES];

and various combinations of it, but it only puts the slider on that element, again not actually selecting it.

Some answers said to apply the above code in the viewDidLoad() method, but unfortunately I'm pulling the location array in from a web service and cannot do that.

Ideally, I'd like to work as - the user clicks in the textbox, the pickerview pops up as normal, if at that point the done button is clicked, the first item is selected.

Thankyou in advance

like image 501
Jonesopolis Avatar asked Jan 30 '13 13:01

Jonesopolis


4 Answers

Swift 3+

func textFieldDidBeginEditing(_ textField: UITextField) {
    if textField == yourTextField{
        self.yourPickerView.selectRow(0, inComponent: 0, animated: true)
        self.pickerView(yourPickerView, didSelectRow: 0, inComponent: 0)
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

//Do all the stuff to populate the TextField

}
like image 109
Dasoga Avatar answered Oct 24 '22 01:10

Dasoga


Here is something you can try :

Set a var in your @interface, something like NSString *selectedString;.

When you init your UIPickerView (in pickerView:titleForRow:forComponent:), when you set the title for the row 0, set selectedString with the same string as the title of the row #0. Then, in pickerView:didSelectRow:inComponent:, set selectedString with the appropiate string :

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

    // Some stuff

    // Assuming "yourDataArray" contains your strings
    selectedString = [yourDataArray objectAtIndex:row];

}

Then, when you call the method triggered by the push of the "done" button, use selectedString to set the text.

Hope this works out for you !

like image 33
rdurand Avatar answered Oct 24 '22 00:10

rdurand


I had a same problem. My solution was this:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Only for the text field with the picker
    if (textField == self.yourTextField)
    {
        // Select the first row programmatically 
        [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
        // The delegate method isn't called if the row is selected programmatically
        [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
    }



}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //Do all the stuff to populate the TextField

}
like image 31
gUIDo Avatar answered Oct 24 '22 00:10

gUIDo


-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    // Only for the text field with the picker
    if (textField == self.yourTextField && [textField length]!=0)
    {
        // Select the first row programmatically 
        [self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
        // The delegate method isn't called if the row is selected programmatically
        [self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
    }



}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //Do all the stuff to populate the TextField
}
like image 35
nithinreddy Avatar answered Oct 24 '22 01:10

nithinreddy