我的应用程序中有 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 调用此方法,则不会发生任何事情。请帮忙=)