调用selectRow:inComponent:animated:方法

时间:2012-09-03 07:06:10

标签: ios5 uipickerview

我的应用程序中有2个UIpicker视图,我希望拾取器在我保存数据后显示最后选择的行。我正在使用这个selectRow:inComponent:animated:方法,但我在哪里可以完全调用这个方法?

在viewDidLoad中:

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

在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;

        }

我在这里有一个方法:

-(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];


}

我该怎么把它放在一起?如果我在例如viewDidLoad中调用此方法,则没有任何反应。请帮助=)

1 个答案:

答案 0 :(得分:1)

我想出了一个主意。因为我的选择器只在调用文本字段时创建项目,即成为第一响应者,因此,我在textFieldDidBeginEditing中调用我的refreshPicker方法。并且不要忘记将textfield委托设置为self。 =)那些遇到我问题的人,欢呼。