gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:12:14 28 4
gpt4 key购买 nike

我正在使用 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;

关于ios - 在 ARC 下,IBOutlets 应该强还是弱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7678469/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com