是否需要保留数据源的控制器和uiPickerview的委托?
据我了解,我不应该保留作为委托或数据源的控制器。我制作了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果选择器实例化了数据源,那么保留它就可以了,但它需要保留在某个地方。请务必释放它。
请注意,数据源的处理方式与委托的处理方式不同。
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.
大多数情况下(据我所知)代表不会被他们的班级保留。它们只是这样分配的,
调用者有责任保留委托,直到委托工作结束。
您的问题的答案是 UIPickerView 不保留其委托。相反,它希望您保留它。
Mostly (as for as I know) the delegates are not retained by their classes. They are just assigned like this,
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.