我使用ARC专门为iOS 5开发游戏。iboutlet到UIViews(和子类)是强还是弱?

以下几点:

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

就能摆脱这一切

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

做这个有什么问题吗?模板使用强,当从“接口生成器”编辑器直接连接到头部时,自动生成的属性创建,但为什么?UIViewController已经有一个强引用到它的视图,它保留了它的子视图。


当前回答

从WWDC 2015开始,有一个关于在界面生成器中实现UI设计的会议。在第32分钟左右,他说你总是想让你的@IBOutlet强大。

其他回答

注意,IBOutletCollection应该是@property(强,非原子)。

虽然文档建议在子视图的属性上使用weak,但从iOS 6开始,使用strong(默认的所有权限定符)似乎是可以的。这是由UIViewController中的变化引起的视图不再被卸载。

在ios6之前,如果你保持强链接到控制器视图的子视图,如果视图控制器的主视图被卸载,只要视图控制器还在,那些就会保留子视图。 自iOS 6以来,视图不再被卸载,而是加载一次,然后只要它们的控制器还在,视图就会一直存在。所以强属性无关紧要。它们也不会创建强引用循环,因为它们向下指向强引用图。

也就是说,我在使用之间犹豫不决

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

and

@property (nonatomic) IBOutlet UIButton *button;

iOS 6及以后版本:

使用weak清楚地表明控制器不想要按钮的所有权。 但是省略weak在iOS 6中没有视图卸载,而且更短。有些人可能会指出,这也更快,但我还没有遇到过因为iboutlet太弱而太慢的应用程序。 不使用weak可能会被认为是错误。

底线:自从iOS 6以来,只要我们不使用视图卸载,我们就不会再犯这个错误了。聚会时间到了。;)

出于性能考虑,IBOutlet应该是强大的。参见iOS 9中的故事板参考,Strong IBOutlet, Scene Dock

As explained in this paragraph, the outlets to subviews of the view controller’s view can be weak, because these subviews are already owned by the top-level object of the nib file. However, when an Outlet is defined as a weak pointer and the pointer is set, ARC calls the runtime function: id objc_storeWeak(id *object, id value); This adds the pointer (object) to a table using the object value as a key. This table is referred to as the weak table. ARC uses this table to store all the weak pointers of your application. Now, when the object value is deallocated, ARC will iterate over the weak table and set the weak reference to nil. Alternatively, ARC can call: void objc_destroyWeak(id * object) Then, the object is unregistered and objc_destroyWeak calls again: objc_storeWeak(id *object, nil) This book-keeping associated with a weak reference can take 2–3 times longer over the release of a strong reference. So, a weak reference introduces an overhead for the runtime that you can avoid by simply defining outlets as strong.

在Xcode 7中,它是强的

如果你观看了WWDC 2015会议407在界面生成器中实现UI设计,它建议(来自http://asciiwwdc.com/2015/sessions/407)

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 sub view 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. So I'm going to choose strong and I will click connect which will generate my outlet.

目前苹果推荐的最佳实践是iboutlet是强的,除非特别需要弱的以避免保留循环。就像Johannes上面提到的,在2015年WWDC的“在界面生成器中实现UI设计”会议上,一位苹果工程师说:

我想指出的最后一个选项是存储类型,它可以 要么坚强,要么软弱。一般来说,你应该做出口 强,特别是当你连接一个输出到子视图或 约束并不总是会被视图保留 层次结构。唯一真正需要使outlet弱的时候是if 您有一个自定义视图,它引用视图中的备份内容 等级制度,一般来说不建议这样做。

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

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

警告,过时的答案:根据WWDC 2015,这个答案不是最新的,正确答案参考上面的公认答案(丹尼尔·霍尔)。这个答案将被记录下来。


从开发人员库总结:

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;