什么时候应该使用弱或强限定符与IBOutlets? [英] When should I use the weak or strong qualifier with IBOutlets?

查看:129
本文介绍了什么时候应该使用弱或强限定符与IBOutlets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

IBOutlets在ARC下是强还是弱?

在文档中,我读到
Outlets通常应该是弱的,除了那些从文件的所有者到顶级对象在nib文件(或在iOS中,一个故事板场景),应该是强。

In the documentation, I read "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."

示例?

推荐答案

一般情况下,我们不知道什么时候更好地使用IBOutlet的weak如果你正在做一个ViewControllers子视图的子视图的出口,它应该是弱的。只要顶视图存在(在viewDidLoad和viewDidUnload之间),对象就存在。由于iOS 5 ARC自动取消了弱链接,当viewController卸载它的视图和视图层次结构被销毁时,你的插座会自动设置为nil。

In general: if you are making an outlet to a subview of the ViewControllers subview it should be weak. The object exists as long as the top view exists (between viewDidLoad and viewDidUnload). As iOS 5 ARC automatically nullifies weak links, when the viewController unloads its view and view hierarchy is destroyed, your outlet is automatically set to nil.

但是,您的nib文件中的另一个对象(模型对象)。由于此对象不在视图层次结构下,您需要使iboutlet强。如果你使它弱链接,该对象将被自动释放,因为没有其他对象有强的引用它,ARC会释放它,并设置为nil到您的IBOutlet。这不是一个子视图的情况,因为它的superview与它有一个强的链接。

But maybe you want to create another object in your nib file (a model object). As this object is not under the view hierarchy, you need to make the iboutlet strong. If you make it weak linked, the object will be autoreleased since no other object has a strong reference to it and ARC will release it and set nil to your IBOutlet. This is not the case of a subview since its superview mantains a strong link with it.

这篇关于什么时候应该使用弱或强限定符与IBOutlets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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