简体   繁体   English

IBOutlets强或弱 - 它是否真的对内存管理产生影响? (弧)

[英]IBOutlets Strong or Weak - Does it Actually Make a Difference to Memory Management? (ARC)

I've been reading a lot recently on here and other sites about whether IBOutlets should be strong or weak. 我最近在这里和其他网站上一直在阅读有关IBOutlets是强还是弱的内容。 The official verdict is that they should be weak, except for when they reference top level xib objects. 官方判决是他们应该是弱者,除非他们引用顶级xib对象。

This is fine. 这可以。

However what I don't yet have a clear understanding of is why setting them to strong or weak actually would make a difference in terms of the app's memory management. 然而,我还没有清楚地理解为什么将它们设置为强或弱实际上会对应用程序的内存管理产生影响。 In particular it seems since iOS6, viewDidUnload is not called on receiving a memory warning anymore, so in this sense the choice of weak or strong for IBOutlets would make no difference. 特别是从iOS6开始,viewDidUnload在接收内存警告时不再被调用,因此从这个意义上说,IBOutlets选择弱或强不会产生任何影响。

Am I right in thinking though that for view controllers no longer on the navigation stack (ie after they have been loaded then popped), that any IBOutlets declared as weak would then be released ? 我是否正确地认为,对于视图控制器不再在导航堆栈上(即在它们被加载然后弹出之后),任何宣布为弱的IBOutlet将被释放?

Thanks for any insights ! 感谢您的任何见解!

You might have noticed that when you insert an IBOutlet directly from Interface Builder you can select whether to have it as a weak or strong property. 您可能已经注意到,当您直接从Interface Builder插入IBOutlet时,您可以选择是将其作为弱属性还是强属性。 After a little search in the Apple developer's library I have found when you should use one or the other: 在Apple开发人员库中进行了一些搜索后,我发现你何时应该使用其中一个:

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通常应该是弱的,除了那些从File的Owner到nib文件中的顶级对象(或者,在iOS中,故事板场景)应该很强。 Outlets that you create should therefore typically be weak, because: 因此,您创建的插座通常应该很弱,因为:

Outlets that you create to subviews of a view controller's view or a window controller's window, for example, 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). 强大的出口通常由框架类指定(例如,UIViewController的视图出口或NSWindowController的窗口出口)。

more 更多

IBOutlets can declared weak because they will be created during XIB parsing and added to the UIView stack...so you don't need a strong reference to the object. IBOutlets可以声明为弱,因为它们将在XIB解析期间创建并添加到UIView堆栈中......因此您不需要对该对象的强引用。

When you declare an IBOutlet weak you ensure that when the main UIView of UIViewcontroller disappear every subview will be deleted without any memory leak. 当您声明IBOutlet弱时,确保当UIViewcontroller的主UIView消失时,将删除每个子视图而不会发生任何内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM