UIPickerView lost data after navigation

41 views Asked by At

I am trying to implement an UIPickerView programmatically. I have implemented the delegate and datasource. When I first navigate to the UIPickerView everything works fine. If I leave the View and come back to it later, the UIPickerView looks fine. But when I try to select an other item it crashes. When I debug it, I saw that my data array is empty. But I don’t know why.

In on class I init the UIPickerView:

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

The Picker is in this Controller:

@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

Here I set the Delegate and Datasource:

-(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;
}

After returning to the View dropDownData is empty.

1

There are 1 answers

0
BHuelse On

I've found the problem. I need to copy the dictionary, not only set a reference.

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