是否需要保留数据源的控制器和uiPickerview的委托?

发布于 2024-12-07 12:30:30 字数 1295 浏览 3 评论 0原文

据我了解,我不应该保留作为委托或数据源的控制器。我制作了一个 UIPickerView,在属性访问器中创建如下:

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

}

此崩溃报告尝试访问 pickerSource 的未分配实例。 如果我将 pickerSource 组件分解为另一个属性,从而将其保留在该控制器中,则它可以完美运行。 我认为这不是正确的实施。 pickerView 不会保留它的委托和数据源直到它被销毁吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单身狗的梦 2024-12-14 12:30:30

如果选择器实例化了数据源,那么保留它就可以了,但它需要保留在某个地方。请务必释放它。

请注意,数据源的处理方式与委托的处理方式不同。

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.

最佳男配角 2024-12-14 12:30:30

大多数情况下(据我所知)代表不会被他们的班级保留。它们只是这样分配的,

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

调用者有责任保留委托,直到委托工作结束。

您的问题的答案是 UIPickerView 不保留其委托。相反,它希望您保留它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文