【发布时间】:2012-03-10 18:51:05
【问题描述】:
我的 .h 文件中有一个属性声明为
@property (weak, nonatomic) UIPickerView *levelPicker;
在我的实现文件中综合为:
@synthesize levelPicker = _levelPicker;
然后我在同一个实现文件中有一个代码块,它执行以下操作:
if (self.levelPicker == nil) {
self.levelPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
self.levelPicker.delegate = self;
self.levelPicker.dataSource = self;
}
textField.inputView = self.levelPicker;
在这种情况下,self._levelPicker 没有设置为新的 UIPickerView。 IE。 self.levelPicker = blah 分配不起作用。
但是,如果我将属性声明更改为:
@property (strong, nonatomic) UIPickerView *levelPicker;
然后一切都按预期工作,_levelPicker 设置为新分配的 UIPickerView。
谁能告诉我为什么会这样?我以为我开始理解参考是如何工作的,但我想我还有更多的东西要学。我阅读了一些其他相关的 SO 帖子,但我仍然不完全清楚。
【问题讨论】:
标签: objective-c ios automatic-ref-counting weak-references