当我通过 Interface Builder 创建 UI 然后附加到 IBOutlets 时,我总是将属性设置为 weak...
@property (nonatomic, weak) IBOutlet UILabel *someLabel;
然后在 UIViewController
的代码中,我可以引用 self.someLabel
或 _someLabel
,它工作正常。
但是,如果我在代码中设置 UI...
@property (nonatomic, weak) UILabel *someLabel;
和...
self.someLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,10,10)];
然后我收到警告...
Assigning retained object to weak property; object will be released after assignment.
我通常的解决方法是将属性更改为 strong
但我不确定这是否正确?
可以吗?如果没有,是否有其他方法可以解决此问题?
最佳答案
My usual fix for this is to just change the property to strong but I'm not sure if this is correct?
这是 100% 正确的。基于 IB 的属性被声明为 weak
的原因是从 NIB 对象创建的 View 已经保留了相应的 UI 元素,因此您不需要从代码中引用 strong
.
关于ios - 使用属性在代码中创建 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16459067/