Search code examples
iosdelegatesuipickerviewretainuipickerviewcontroller

Necessary to retain controller for datasource and delegate of uiPickerview?


As I understood, I should not be retaining a controller which is a delegate or datasource. I have made a UIPickerView, created in a property accessor as such:

-(UIPickerView *)projectPicker {
  if (_projectPicker != nil) {
      return _projectPicker;
  }

  //Create Picker View
  UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 185, 0, 0)];
  picker.showsSelectionIndicator = YES;
  //Create source and delegate
  NSString *titleForRow0 = NSLocalizedString(@"<<Make Selection>>", @"projectPicker nil Label 0");
  NSArray *titlesForFirstRows = [[NSArray alloc] initWithObjects:titleForRow0, nil];
  ProjectPickerDatasource *pickerSource = [[ProjectPickerDatasource alloc] initWithManagedObjectContext:self.managedObjectContext
                                                                                      selectedProject:self.currentProject
                                                                                andTitlesForFirstRows:titlesForFirstRows];
  [titlesForFirstRows release];
  picker.delegate = pickerSource;
  picker.dataSource = pickerSource;

  self.projectPicker = picker;

  [pickerSource release];
  [picker release];


  return _projectPicker;

}

This crashes reporting an attempt to access an unallocated instance of pickerSource. If I break the pickerSource component out as another property, thereby retaining it within this controller, it works perfectly. I did not think that was the proper implementation. Doesn't the pickerView retain it's delegate and datasource until it is destroyed?


Solution

  • If the Picker instantiates the datasource it is fine to retain it, it needs to be retained somewhere. Just be sure to release it.

    Note, datasources are handled differently that delegates.