ios - 使用属性在代码中创建 UI

标签 ios cocoa-touch properties

当我通过 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/

相关文章:

iphone - iOS - "Too Many Contexts"错误?

ios - 在哪里可以找到 iOS 7 的默认后退按钮 V 形图像?

java - Spring Boot - application.properties 中的程序参数

ios - appInviteDialog didCompleteWithResults 委托(delegate)方法中的 EXC_BREAKPOINT | Facebook 应用程序邀请

ios - 在 Storyboard预览 xcode 中获取 "xxx.storyboard is currently locked because it is a remote resource"

iphone - 我如何知道点击了哪个按钮? objective-c

iOS 7 淡入淡出状态栏文本?

iphone - 我需要释放xib资源吗?

java - java .properties 文件值中的换行符

objective-c - @property 声明属性的顺序是否有公认的约定?