IBOutlets在ARC下是强还是弱? [英] Should IBOutlets be strong or weak under ARC?

查看:26
本文介绍了IBOutlets在ARC下是强还是弱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing exclusively for iOS 5 using ARC. Should IBOutlets to UIViews (and subclasses) be strong or weak?

以下内容:

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

将摆脱所有这些:

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

这样做有什么问题吗?模板使用 strong 以及从Interface Builder"编辑器直接连接到标题时创建的自动生成的属性,但为什么呢?UIViewController 已经有一个对它的 viewstrong 引用,它保留了它的子视图.

Are there any problems doing this? The templates are using strong as are the automatically generated properties created when connecting directly to the header from the 'Interface Builder' editor, but why? The UIViewController already has a strong reference to its view which retains its subviews.

推荐答案

Apple 当前推荐的最佳实践是让 IBOutlets strong 除非特别需要 weak避免保留循环.正如上面提到的 Johannes,在 WWDC 2015 的在界面生成器中实现 UI 设计"会议中对此进行了评论,一位 Apple 工程师说:

The current recommended best practice from Apple is for IBOutlets to be strong unless weak is specifically needed to avoid a retain cycle. As Johannes mentioned above, this was commented on in the "Implementing UI Designs in Interface Builder" session from WWDC 2015 where an Apple Engineer said:

我想指出的最后一个选项是存储类型,它可以要么强要么弱.一般来说,你应该让你的出口强大,尤其是当您将插座连接到子视图或视图并不总是保留的约束等级制度.你唯一真正需要削弱出口的时候是如果您有一个自定义视图,该视图引用了该视图的备份内容层次结构,一般不推荐这样做.

And the last option I want to point out is the storage type, which can either be strong or weak. In general you should make your outlet strong, especially if you are connecting an outlet to a subview or to a constraint that's not always going to be retained by the view hierarchy. The only time you really need to make an outlet weak is if you have a custom view that references something back up the view hierarchy and in general that's not recommended.

我在 Twitter 上向 IB 团队的一名工程师询问了这个问题,他确认 strong 应该是默认设置,并且开发人员文档正在更新.

I asked about this on Twitter to an engineer on the IB team and he confirmed that strong should be the default and that the developer docs are being updated.

https://twitter.com/_danielhall/status/620716996326350848https://twitter.com/_danielhall/status/620717252216623104

这篇关于IBOutlets在ARC下是强还是弱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆