我正在使用 ARC 专门为 iOS 5 开发。 IBOutlet
s 到 UIView
s(和子类)应该是 strong
还是 weak
?
以下内容:
@property (nonatomic, weak) IBOutlet UIButton *button;
将摆脱所有这些:
- (void)viewDidUnload
{
// ...
self.button = nil;
// ...
}
这样做有什么问题吗?模板使用 strong
以及从“Interface Builder”编辑器直接连接到标题时创建的自动生成的属性,但为什么呢? UIViewController
已经有一个对其 view
的 strong
引用,该引用保留了它的 subview 。
最佳答案
警告,过时的答案:根据 WWDC 2015,此答案不是最新的,正确答案请参阅 accepted answer (丹尼尔霍尔)以上。此答案将留作记录。
总结自 developer library :
From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create will therefore typically be weak by default, because:
Outlets that you create to, for example, subviews of a view controller’s view or a window controller’s window, are arbitrary references between objects that do not imply ownership.
The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).
@property (weak) IBOutlet MyView *viewContainerSubview; @property (strong) IBOutlet MyOtherClass *topLevelObject;
https://stackoverflow.com/questions/7678469/