开发者

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 reporti开发者_运维知识库ng 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?


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.


Mostly (as for as I know) the delegates are not retained by their classes. They are just assigned like this,

@property(nonatomic, assign) id <TheDelegateClass> delegate;

Its the responsibility of the caller to retain the delegate until the delegates job is over.

The answer for your question is UIPickerView doesn't retain its delegate. It expects you to retain it instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜