首先看一下官方文档的介绍Managing the Lifetimes of Objects from Nib Files
Each time you ask the NSBundle or NSNib class to load a nib file, the underlying code creates a new copy of the objects in that file and returns them to you. (The nib-loading code does not recycle nib file objects from a previous load attempt.) You need to ensure that you maintain the new object graph as long as necessary, and disown it when you are finished with it. You typically need strong references to top-level objects to ensure that they are not deallocated; you don’t need strong references to objects lower down in the graph because they’re owned by their parents, and you should minimize the risk of creating strong reference cycles.
用谷歌大致翻译了一下: 每次请求NSBundle或NSNib类加载nib文件时,底层代码都会在该文件中创建对象的新副本并将它们返回给您。 (nib加载代码不会从先前的加载尝试中回收nib文件对象。)您需要确保在必要时维护新的对象图,并在完成它时将其取消。您通常需要对顶级对象的强引用,以确保它们不会被释放;您不需要对图表中较低的对象进行强引用,因为它们由父母拥有,您应该尽量减少创建强引用周期的风险。
-
因为当我们将控件拖到Storyboard上,相当于新创建了一个对象,只是关联到viewcontroller,而不是添加到viewcontroller.这个对象是加到viewcontroller的view上,view有一个subViews属性,[self.view addSubView:xxx], 这个属性是一个数组,里面是这个views的所有子view,而我们加的控件就位于这个数组中,那么说明,实际上我们的控件对象是属于views的,也就是说views对加到它上面的控件是强引用, 这个时候self.view已经对xxx 强引用过了,self.view才是持有xxx的对象。这样子才符合引用计数的规则。所以直接IBOutlet顶级view的时候肯定是strong的。当我们使用Outlet属性的时候,我们是在viewController里面使用,而这个Outlet属性是有view来进行强引用的,我们在viewController里面仅仅是对其使用,并没有必要拥有它,所以这个weak是views里面的子view对于viewController来说的.
-
如果将weak改为strong,也是没有问题的,如果非要说有什么问题,官方文档最后 “and you should minimize the risk of creating strong reference cycles.” 您应该尽量减少创建强引用周期的风险。也就是说,没有必要做可能会产生风险的事情,对于一个view也就是使用周期风险. 官方文档下面还有一句: The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).意思就是从官方框架和使用模式上考虑这是最适合的,最好的方式.