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

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

问题描述

我正在使用 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 当前推荐的最佳实践是让 IBOutletsstrong,除非 weak 特别需要避免保留循环.正如 Johannes 上面提到的,这在 WWDC 2015 的在 Interface Builder 中实现 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

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

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