ios - 在 ARC 下,IBOutlets 应该强还是弱?

我正在使用 ARC 专门为 iOS 5 开发。 IBOutlets 到 UIViews(和子类)应该是 strong 还是 weak

以下内容:

@property (nonatomic, weak) IBOutlet UIButton *button;

将摆脱所有这些:

- (void)viewDidUnload
{
    // ...
    self.button = nil;
    // ...
}

这样做有什么问题吗?模板使用 strong 以及从“Interface Builder”编辑器直接连接到标题时创建的自动生成的属性,但为什么呢? UIViewController 已经有一个对其 viewstrong 引用,该引用保留了它的 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/

相关文章:

objective-c - 如何迭代 NSArray?

objective-c - 在 Objective-C 中创建一个抽象类

objective-c - 如何将百分号添加到 NSString

objective-c - 如何打印出方法名称和行号并有条件地禁用 NSLog?

ios - 如何浏览文本字段(下一步/完成按钮)

objective-c - 如何检查 NSDictionary 或 NSMutableDiction

objective-c - 如何在 Xcode 4 中设置 NSZombieEnabled?

ios - 如何将 UIButton 的标题设置为左对齐?

ios - 如何以编程方式在 iPhone 上发送短信?

ios - 设置 UITextField 的最大字符长度