IBOutlets应该在ARC下是强还是弱?

编程入门 行业动态 更新时间:2024-10-25 12:16:08
本文介绍了IBOutlets应该在ARC下是强还是弱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发专为iOS 5使用ARC。应 IBOutlet 到 UIView (和子类) 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 已有 strong 引用其视图

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强力 weak 特别需要避免保留周期。正如Johannes上面提到的,这是在WWDC 2015的实现UI设计UI界面构建器会话中被评论的,一个苹果工程师说:

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.

twitter/_danielhall/status/620716996326350848 twitter/_danielhall/status/620717252216623104

更多推荐

IBOutlets应该在ARC下是强还是弱?

本文发布于:2023-10-27 11:42:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:IBOutlets   ARC   下是强

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!