导航后UIPickerView丢失了数据

时间:2014-11-19 17:25:01

标签: ios navigation delegates malloc alloc

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

在课堂上我初始化UIPickerView

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

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为空。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。我需要复制字典,不仅要设置参考。

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