我正在尝试为UIPickerView设置默认值。但是,无论我将其设置为哪个值,它始终默认为1。下面的方法可以很好地加载选择器,我可以选择它。那么,我在做什么错呢?这是代码。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 1 "];
[arrayNo addObject:@" 2 "];
[arrayNo addObject:@" 3 "];
[arrayNo addObject:@" 4 "];
[arrayNo addObject:@" 5 "];
[pickerView selectRow:4 inComponent:0 animated:YES];
mlabel.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];
}
Picker的DataSource方法:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
mlabel.text = [arrayNo objectAtIndex:row];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:[arrayNo objectAtIndex:row]];
[f release];
prefs = [NSUserDefaults standardUserDefaults];
NSInteger myInteger = [myNumber integerValue];
// save level
[prefs setInteger:myInteger forKey:@"myInt"];
// saving it all
[prefs synchronize];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayNo count];
}
最佳答案
如果我把它放在这种方法中,它将起作用:
-(void) viewWillAppear: (BOOL) animated