Ios UIPickerView在导航后丢失数据

Ios UIPickerView在导航后丢失数据,ios,navigation,delegates,malloc,alloc,Ios,Navigation,Delegates,Malloc,Alloc,我试图以编程方式实现UIPickerView。我已经实现了委托和数据源。 当我第一次导航到UIPickerView时,一切正常。如果我离开视图,稍后再返回,UIPickerView看起来很好。 但当我尝试选择其他项目时,它崩溃了。 当我调试它时,我看到我的数据数组是空的。但我不知道为什么 在类I init上的UIPickerView中: 选择器位于该控制器中: @interface DropDownController : UIViewController <FormElement, UI

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

在类I init上的UIPickerView中:

选择器位于该控制器中:

@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

返回视图后,下拉数据为空。

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

// dropDownData = dataDictionary; -> wrong
dropDownData = [dataDictionary copy];
-(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 = dataDictionary; -> wrong
dropDownData = [dataDictionary copy];