[英]PickerView shows up with no options, even though I have set them
I have a UIPickerView, and it is showing up, but it just shows (null). 我有一个UIPickerView,它正在显示,但仅显示(空)。 Why is this?
为什么是这样? I have it conformed to the delegate's I need in.
我已使其符合我需要的代表的要求。
EDIT: more detail. 编辑:更多细节。
Basically, my app is a flight logbook. 基本上,我的应用是飞行日志。 You make planes, which you saves, and you can pick it when you make a new session.
您可以创建飞机并保存,然后在进行新的会话时可以选择它。 The planeNum key stores how many planes there are, and the key Plane%liTailNumber has the tail number for that plane.
planeNum键存储有多少个平面,键Plane%liTailNumber具有该平面的尾号。
This is my code: 这是我的代码:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return (long)[[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(@"%@", tailPickerOptions);
}
return tailPickerOptions[row];
}
You initialize a new array for each loop of the while. 您为while的每个循环初始化一个新的数组。 Your code also doesn't ensure you actually initialize the array - if num = 0, you'll never go in and declare the array.
您的代码也不能确保您实际初始化数组-如果num = 0,则永远不会进入并声明该数组。 There should be a warning or compiler error for this.
为此,应该有一个警告或编译器错误。 In the best case scenario because you keep declaring a new array in your loop, you'll return a max of 1 element ever.
在最佳情况下,因为您不断在循环中声明新数组,所以您最多将返回1个元素。
Move your array declaration above the while loop. 将数组声明移到while循环上方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.