【发布时间】:2016-01-04 14:36:11
【问题描述】:
当不使用界面生成器时,我总是保持对 UI 元素的强引用:
@interface myViewController : UIViewController
@property (nonatomic, strong) UILabel *folderLabel;
然后像这样添加它们:
[self.view addSubview self.folderLabel];
初始化程序在哪里:
-(UILabel *)folderLabel{
if(!_folderLabel) {
_folderLabel = [[UILabel alloc] init];
_folderLabel.text = @"foo";
}
return _folderLabel
}
有人告诉我,由于某种原因这很糟糕,他们应该总是很弱..
@property (nonatomic, weak) UILabel *folderLabel;
-(UILabel *)folderLabel{
if(!_folderLabel) {
UIlabel *folderLabel = [[UILabel alloc] init];
folderLabel.text = @"foo";
[self.view addSubview:folderLabel];
_folderLabel = folderLabel;
}
return _folderLabel
}
强引用在这里是件坏事吗?
【问题讨论】:
-
请注意,在该问题上投票较高的答案而不是接受的答案是正确的
-
那个问题是指 IBoulets,这是以编程方式设置 UI 元素
-
投票率最高的答案似乎与有关苹果建议保持强大的新信息过时了..
-
IBOutlet 只是句法修饰,让NIB绑定进程能够找到属性。元素的创建方式无关紧要
标签: ios objective-c user-interface weak