Search code examples
ios5uipickerview

calling selectRow:inComponent:animated: method


I have 2 UIpicker view in my app and I want the pickers to show the last chosen row after i saved my data. I am using this selectRow:inComponent:animated: method, but where do I exactly call this method?

in viewDidLoad:

if (self.pickerView == nil) {
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator = YES;

in pickerView:viewForRow:forComponent:reusingView:

if (component == 0)
        {
            for (int j =20; j<=200; j++)
            {
            NSString* myString           = [NSString stringWithFormat:@"%d",j];
            [myArray addObject:myString];
            }
            UILabel *weightLabel0        = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 100, 32)];
            Label0.text            = [myArray objectAtIndex:row];
            Label0.textAlignment   = UITextAlignmentLeft;
            Label0.backgroundColor = [UIColor clearColor];
            [rowView insertSubview:weightLabel0 atIndex:1];
return rowView;

        }

and i have a method here:

-(void) refreshPicker
{


    NSArray* mArray = [self.myTextField.text componentsSeparatedByString:@"."];
    NSNumber* str1 = [mArray objectAtIndex:0];
    int selectRow1 = [str1 intValue]-20;
    NSNumber* str2 = [mArray objectAtIndex:1];
    int selectRow = [str2 intValue];

    if (selectRow == 0) {
        int selectRow2 = 0;
        [self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
    }
    else if (selectRow == 5) {
        int selectRow2 = 1;
        [self.pickerView selectRow:selectRow2 inComponent:1 animated:NO];
    }

    [self.pickerView  selectRow:selectRow1 inComponent:0 animated:NO];

    [pickerView_ reloadAllComponents];


}

how should i put this together? if i call this method at, for instance, viewDidLoad, nothing happens. help please =)


Solution

  • I have came up with an idea. since my pickers only create the items when the textfield is being called, i.e. becomes first responder, hence, i call my refreshPicker method inside textFieldDidBeginEditing. and don't forget to set the textfield delegate to self. =) to those who encounters my problem, cheers.