UIPickerView 导航后丢失数据



我正在尝试以编程方式实现UIPickerView。我已经实现了委托和数据源。当我第一次导航到UIPickerView时,一切正常。如果我离开View,稍后再回来,UIPickerView看起来很好。但是当我尝试选择其他项目时,它会崩溃。当我调试它时,我看到我的数据数组是空的。但我不知道为什么。

在课堂上,我初始化了UIPickerView

DropDownController *objPickerView = [[DropDownController alloc] init];
objPickerView.userInfo = userInfo;
[objPickerView setDataSourceForPickerView:[dropDownItem valueForKey:@"dropDownEntries"] withPreselectedItem:preSelectedItem];
[dropDownContainer addSubview:objPickerView.picker];

选取器位于此控制器中:

@interface DropDownController : UIViewController <FormElement, UIPickerViewDelegate, UIPickerViewDataSource>
    {
        NSArray *dropDownData;
        UIPickerView *picker;
        UIElement *userInfo;
    }
    @property (strong, nonatomic) NSArray *dropDownData;
    @property (strong, nonatomic) IBOutlet UIPickerView *picker;
    @property (nonatomic, retain) UIElement *userInfo;
-(void)setDataSourceForPickerView:(NSArray *)dataDictionary withPreselectedItem:(NSString*) preSelectedItem;

@end

在这里,我设置委托和数据源:

-(void)setDataSourceForPickerView:(NSMutableArray *)dataDictionary withPreselectedItem:(NSString*) preSelectedItem{
    picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 300, 162)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    dropDownData = dataDictionary;
}

返回到视图后dropDownData为空。

我发现了问题。我需要复制字典,而不仅仅是设置引用。

// dropDownData = dataDictionary; -> wrong
dropDownData = [dataDictionary copy];

相关内容

  • 没有找到相关文章

最新更新