我有一个UIPickerView,只要我尝试打开它就会崩溃。我希望用户能够从他们制作的飞机上选择尾号。 这是我的代码:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
NSLog(@"%i", (int)[[NSUserDefaults standardUserDefaults]integerForKey:@"planeNum"]);
return (NSInteger)[[NSUserDefaults standardUserDefaults]integerForKey:@"planeNum"];
}
NSMutableArray *tailPickerOptions;
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSInteger num = [[NSUserDefaults standardUserDefaults]integerForKey:@"planeNum"];
while (num > 0) {
--num;
tailPickerOptions = [[NSMutableArray alloc]init];
NSString *dTailNumber = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"Plane%liTailNumber", (long)num]]];
[tailPickerOptions addObject:dTailNumber];
NSLog(@"%@", dTailNumber);
}
return tailPickerOptions[row];
}
答案 0 :(得分:1)
代码行:
tailPickerOptions = [[NSMutableArray alloc]init];
导致tailPickerOptions
多次初始化(每次通过while
循环一次),因此只会有一个对象(在索引0处,或者,例如," bounds [ 0..0]&#34)。将其移到while
循环之外。例如,
tailPickerOptions = [[NSMutableArray alloc]init];
while (num > 0) {
--num;
NSString *dTailNumber = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:@"Plane%liTailNumber", (long)num]]];
[tailPickerOptions addObject:dTailNumber];
NSLog(@"%@", dTailNumber);
}