什么时候应该对IBOutlets使用Strong vs Weak(进一步说明)

时间:2021-03-14 17:08:38

I thought I understood it clearly from this question --> Should IBOutlets be strong or weak under ARC? but I recently had a discussion which left me totally confused. Can someone just confirm if the following is correct? (if this is a duplicate I didn't mean to break any rules.. just need clarification as I can understand diagrams better than words..)

我认为我从这个问题中清楚地理解了这一点 - >在ARC下IBOutlets是强还是弱?但我最近进行了一次讨论,让我完全糊涂了。有人可以确认以下是否正确? (如果这是一个副本,我并不打算违反任何规则..只需要澄清,因为我可以理解图表比文字更好..)

什么时候应该对IBOutlets使用Strong vs Weak(进一步说明)

Under ARC (MacOSx)

ARC下(MacOSx)

  1. view1 = strong
  2. view1 =强
  3. MainView = weak (In WindowControllerA)
  4. MainView = weak(在WindowControllerA中)
  5. MainView = strong (In ViewControllerB)
  6. MainView = strong(在ViewControllerB中)
  7. view2 = strong
  8. view2 =强
  9. view3 = weak (In ViewcontrollerB)
  10. view3 = weak(在ViewcontrollerB中)
  11. view3 = strong (In ViewControllerC)
  12. view3 = strong(在ViewControllerC中)

If this is correct then can someone confirm please..

如果这是正确的,那么有人可以确认..

In the diagram above, we have a windowControllerA that is on the screen. In windowControllerA's view, there are 2 NSViews. view1 belongs to the windowController, but mainView belongs to the view of the instianciated viewController, ViewControllerB.

在上图中,我们在屏幕上有一个windowControllerA。在windowControllerA的视图中,有2个NSView。 view1属于windowController,但是mainView属于instiociated viewController,ViewControllerB的视图。

ViewControllerB also contains 2 views within its mainView. View2 is owned by viewControllerB while view3 belongs to another instanced viewController, ViewControllerC.

ViewControllerB在其mainView中还包含2个视图。 View2由viewControllerB拥有,而view3属于另一个实例viewController,ViewControllerC。

ViewController C has one view which it owns.

ViewController C有一个它拥有的视图。

4 个解决方案

#1


19  

Most outlets for subviews don't need to be strong references because, after all, they're subviews loaded as part of a view hierarchy. As long as the top-level view exists, and as long as you don't go removing the subviews from their parents, the subviews in the view hierarchy will be retained by their parents for the lifetime of the top-level view.

子视图的大多数出口不需要是强引用,因为毕竟它们是作为视图层次结构的一部分加载的子视图。只要*视图存在,并且只要您不从父项中删除子视图,视图层次结构中的子视图将由其父级保留在*视图的生命周期中。

In the days before ARC, some people were happy to rely on the view hierarchy to retain their views for them and so set their outlet properties to assign. Others didn't like the idea that a hiccup in the view hierarchy could leave them with a handful of dangling pointers, and so set their properties to retain. ARC gives us zeroing weak references, so that your outlets will be set to nil if the objects they point to are deallocated, and that makes using weak references for outlets seem a lot safer. On the other hand, if you want to maintain a reference to a view even if the view hierarchy that contains is is deallocated, you should set that reference to strong.

在ARC之前的日子里,有些人乐于依赖视图层次结构来保留他们的视图,因此设置他们的出口属性来分配。其他人不喜欢视图层次结构中的打嗝可能会让他们留下一些悬空指针的想法,因此将其属性设置为保留。 ARC给我们归零弱引用,这样如果它们指向的对象被释放,你的出口将被设置为nil,这使得使用弱引用的出口似乎更安全。另一方面,如果要保留对视图的引用,即使已取消分配包含的视图层次结构,也应将该引用设置为strong。

Since your view controller is responsible for the view hierarchy it manages (i.e. "owns"), it should have a strong reference to the top-level view. You don't need to worry too much about this, as the view property of any UIViewController-derived view controller is set to retain (i.e. strong).

由于您的视图控制器负责它管理的视图层次结构(即“拥有”),因此它应该具有对顶层视图的强引用。您不必过于担心这一点,因为任何UIViewController派生的视图控制器的视图属性都设置为保留(即强)。

#2


2  

As long as I understood this "All the top-level objects should be strong. And subviews should be weak". So in that case view2 should be weak also

只要我理解这一点“所有*对象都应该很强大。而子视图应该很弱”。所以在这种情况下,view2也应该是弱的

  • view1 = strong
  • view1 =强
  • MainView = weak (In WindowControllerA)
  • MainView = weak(在WindowControllerA中)
  • MainView = strong (In ViewControllerB)
  • MainView = strong(在ViewControllerB中)
  • view2 = weak (since mainview already hold it)
  • view2 = weak(因为mainview已经拥有它)
  • view3 = weak (In ViewcontrollerB)
  • view3 = weak(在ViewcontrollerB中)
  • view3 = strong (In ViewControllerC)
  • view3 = strong(在ViewControllerC中)

#3


0  

So many discussions about the weak vs. strong reference to .xib objects in the File Owner's IBOutlets --- and it seems EVERYONE is only concerned about views and subviews.

关于文件所有者的IBOutlets中.xib对象的弱与强引用的讨论很多---而且似乎每个人都只关注视图和子视图。

Subviews are owned by their superviews, and so as long as you do not tear down view hierarchy programmatically (and take responsibility for view ownership while at it) you don't need to worry too much.

子视图由其超级视图拥有,因此只要您不以编程方式拆除视图层次结构(并在其中负责查看所有权),您就不必过于担心。

BUT --- what about all those other objects you create in .xibs regularly, like NSArrayControllers and those root-level UI items that are NOT views, like Windows, Panels, Alerts, and so on --- should THEY be referenced strong? weak?

但是 - 你经常在.xibs中创建的所有其他对象怎么样呢,比如NSArrayControllers和那些非视图的根级UI项目,比如Windows,Panels,Alerts等等,它们应该被引用得强吗?弱?

I really need some low level explanation of how xibs work. When an object is a "File's owner" and loads its nib file, What gets loaded and initialized? Just objects to which you have IBOutlets? Every top-level object?

我真的需要一些关于xib如何工作的低级别解释。当一个对象是“文件的所有者”并加载其nib文件时,会加载和初始化什么?只是你有IBOutlets的对象?每个*对象?

Who owns all these root-level objects? After all the controller who loads the nib (and is normally the "File's Owner" owns the .xib --- but does this mean it owns (automatically) the root level objects in the nib?

谁拥有所有这些根级对象?在所有加载笔尖的控制器(通常是“文件所有者”)拥有.xib之后---这是否意味着它(自动)拥有笔尖中的根级对象?

If so --- what is the difference if you have a weak IBOutlet reference to a .xib object, or strong, or none at all ---- you're still "File's Owner" for that object,

如果是这样的话---如果您对.xib对象的弱IBOutlet引用,或者强或者根本没有引用,则有什么区别----您仍然是该对象的“文件所有者”,

Some better explanation will do.

一些更好的解释会做。

#4


0  

If I set the property of "view1","MainView (In ViewControllerB)" and "view3 (In ViewControllerC)" to weak, then what will happen? Please explain.

如果我将“view1”,“MainView(在ViewControllerB)”和“view3(在ViewControllerC中)”的属性设置为弱,那么会发生什么?请解释。

#1


19  

Most outlets for subviews don't need to be strong references because, after all, they're subviews loaded as part of a view hierarchy. As long as the top-level view exists, and as long as you don't go removing the subviews from their parents, the subviews in the view hierarchy will be retained by their parents for the lifetime of the top-level view.

子视图的大多数出口不需要是强引用,因为毕竟它们是作为视图层次结构的一部分加载的子视图。只要*视图存在,并且只要您不从父项中删除子视图,视图层次结构中的子视图将由其父级保留在*视图的生命周期中。

In the days before ARC, some people were happy to rely on the view hierarchy to retain their views for them and so set their outlet properties to assign. Others didn't like the idea that a hiccup in the view hierarchy could leave them with a handful of dangling pointers, and so set their properties to retain. ARC gives us zeroing weak references, so that your outlets will be set to nil if the objects they point to are deallocated, and that makes using weak references for outlets seem a lot safer. On the other hand, if you want to maintain a reference to a view even if the view hierarchy that contains is is deallocated, you should set that reference to strong.

在ARC之前的日子里,有些人乐于依赖视图层次结构来保留他们的视图,因此设置他们的出口属性来分配。其他人不喜欢视图层次结构中的打嗝可能会让他们留下一些悬空指针的想法,因此将其属性设置为保留。 ARC给我们归零弱引用,这样如果它们指向的对象被释放,你的出口将被设置为nil,这使得使用弱引用的出口似乎更安全。另一方面,如果要保留对视图的引用,即使已取消分配包含的视图层次结构,也应将该引用设置为strong。

Since your view controller is responsible for the view hierarchy it manages (i.e. "owns"), it should have a strong reference to the top-level view. You don't need to worry too much about this, as the view property of any UIViewController-derived view controller is set to retain (i.e. strong).

由于您的视图控制器负责它管理的视图层次结构(即“拥有”),因此它应该具有对顶层视图的强引用。您不必过于担心这一点,因为任何UIViewController派生的视图控制器的视图属性都设置为保留(即强)。

#2


2  

As long as I understood this "All the top-level objects should be strong. And subviews should be weak". So in that case view2 should be weak also

只要我理解这一点“所有*对象都应该很强大。而子视图应该很弱”。所以在这种情况下,view2也应该是弱的

  • view1 = strong
  • view1 =强
  • MainView = weak (In WindowControllerA)
  • MainView = weak(在WindowControllerA中)
  • MainView = strong (In ViewControllerB)
  • MainView = strong(在ViewControllerB中)
  • view2 = weak (since mainview already hold it)
  • view2 = weak(因为mainview已经拥有它)
  • view3 = weak (In ViewcontrollerB)
  • view3 = weak(在ViewcontrollerB中)
  • view3 = strong (In ViewControllerC)
  • view3 = strong(在ViewControllerC中)

#3


0  

So many discussions about the weak vs. strong reference to .xib objects in the File Owner's IBOutlets --- and it seems EVERYONE is only concerned about views and subviews.

关于文件所有者的IBOutlets中.xib对象的弱与强引用的讨论很多---而且似乎每个人都只关注视图和子视图。

Subviews are owned by their superviews, and so as long as you do not tear down view hierarchy programmatically (and take responsibility for view ownership while at it) you don't need to worry too much.

子视图由其超级视图拥有,因此只要您不以编程方式拆除视图层次结构(并在其中负责查看所有权),您就不必过于担心。

BUT --- what about all those other objects you create in .xibs regularly, like NSArrayControllers and those root-level UI items that are NOT views, like Windows, Panels, Alerts, and so on --- should THEY be referenced strong? weak?

但是 - 你经常在.xibs中创建的所有其他对象怎么样呢,比如NSArrayControllers和那些非视图的根级UI项目,比如Windows,Panels,Alerts等等,它们应该被引用得强吗?弱?

I really need some low level explanation of how xibs work. When an object is a "File's owner" and loads its nib file, What gets loaded and initialized? Just objects to which you have IBOutlets? Every top-level object?

我真的需要一些关于xib如何工作的低级别解释。当一个对象是“文件的所有者”并加载其nib文件时,会加载和初始化什么?只是你有IBOutlets的对象?每个*对象?

Who owns all these root-level objects? After all the controller who loads the nib (and is normally the "File's Owner" owns the .xib --- but does this mean it owns (automatically) the root level objects in the nib?

谁拥有所有这些根级对象?在所有加载笔尖的控制器(通常是“文件所有者”)拥有.xib之后---这是否意味着它(自动)拥有笔尖中的根级对象?

If so --- what is the difference if you have a weak IBOutlet reference to a .xib object, or strong, or none at all ---- you're still "File's Owner" for that object,

如果是这样的话---如果您对.xib对象的弱IBOutlet引用,或者强或者根本没有引用,则有什么区别----您仍然是该对象的“文件所有者”,

Some better explanation will do.

一些更好的解释会做。

#4


0  

If I set the property of "view1","MainView (In ViewControllerB)" and "view3 (In ViewControllerC)" to weak, then what will happen? Please explain.

如果我将“view1”,“MainView(在ViewControllerB)”和“view3(在ViewControllerC中)”的属性设置为弱,那么会发生什么?请解释。