Why is addSubview: not retaining the view?

前端 未结 3 1205
醉话见心
醉话见心 2020-12-15 12:23

I\'m adding two subviews, which are stored by properties, to my view. When adding the subviews to my view the subViews are seemingly deallocated after my setup

3条回答
  •  情话喂你
    2020-12-15 13:07

    Weak shouldn't be used unless in cases where a parent-child retain cycle is formed (the parent keeps a reference to the child, the child keeps a reference to the parent, so neither is dealloc'd). Strong is the ARC equivalent of retain (now invalid under ARC), and keeps a nice stable pointer to the object for a much longer period of time than a weak reference, hence addSubview actually works instead of giving you some kind of error.

    Why is the addSubview: and insertSubview: not retaining the subviews?

    Have you ever tried to call retain on a nil object? Yep, still nil. Your weak reference isn't keeping the UIView object around long enough to successfully 'call retain' on the object.

提交回复
热议问题