Why is addSubview: not retaining the view?

前端 未结 3 1201
醉话见心
醉话见心 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:14

    Here's a line of your code:

    self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
    

    Recall that the horizontalLineView property is weak. Let's walk through what really happens in that line, with the extra code that ARC generates. First, you send the alloc and initWithFrame: methods, getting back a strong reference:

    id temp = [[HorizontalLineView alloc] initWithFrame:self.frame];
    

    At this point, the HorizontalLineView object has a retain count of 1. Next, because you used dot-syntax to set the horizontalLineView property, the compiler generates code to send the setHorizontalLineView: method to self, passing temp as the parameter. Since the HorizontalLineView property is declared weak, the setter method does this:

    objc_storeWeak(&self->_horizontalLineView, temp);
    

    That sets self->_horizontalLineView equal to temp, and puts &self->_horizontalLineView on the object's list of weak references. But it does not increment the retain count of the HorizontalLineView object.

    Finally, because the temp variable is no longer needed, the compiler generates this:

    [temp release];
    

    That lowers the HorizontalLineView object's retain count to zero, so it deallocates the object. During deallocation, it walks down the list of weak references, and sets each one to nil. So self->_horizontalLineView becomes nil.

    The way to fix this is to make the temp variable explicit, so that you can extend its lifetime until after you have added the HorizontalLineView object to its superview, which retains it:

    HorizontalLineView *hlv = [[HorizontalLineView alloc] initWithFrame:self.frame];
    self.horizontalLineView = hlv;
    // ...
    [self insertSubview:hlv atIndex:0];
    

提交回复
热议问题