简体   繁体   English

即使我已设置PickerView,也没有任何选项

[英]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.

相关问题 即使我添加了约束,这些约束也没有出现在Xcode 6.3中 - Constraints not showing up in Xcode 6.3 even though I added them 带有自定义图像的 UIButton 仍然显示 titleLabel,即使我将其设置为空白 - Swift iOS - UIButton with custom image still shows titleLabel even though I set it to blank - Swift iOS 即使我在NSDateFormatter上设置了时区,为什么stringFromDate NSString仍然具有GMT时间? - Why does stringFromDate NSString still have GMT time even though I set timezone on the NSDateFormatter? UITableView 要么没有被调用,即使我已经设置了它的委托、数据源并注册了可重用的单元格 - UITableView either not being called even though I have set its delegate, dataSource, and registered the reusable cell 我想将 pickerView 设置为默认值 - I want to set pickerView to a default value 即使添加了代理,iAd也无法正常工作 - iAd not working even though I have added the delegate 即使我有navigationController,pushViewController也无法正常工作 - pushViewController doesnt work even though i have a navigationController 即使我有一个“ if”语句检查它是否不是,该变量为什么还是“ nil” - Why is this variable “nil” even though I have an “if” statement that checks it not to be PickerView仅显示“?” - PickerView shows only '?' 即使我在情节提要中设置了segue,也从未调用prepareForSegue - prepareForSegue never got called even though I set the segue in storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM