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 method is called. The end result is that the views never get displayed. Now, if I change my properties to strong as opposed to weak I retain a reference to the views and they now show on screen. So what’s going on here? Why is the addSubview: and insertSubview: not retaining the subviews? See the code below:
btw, I’m using iOS5 with ARC (hence the strong and weak stuff)
#import "NoteView.h"
@interface NoteView() <UITextViewDelegate>
@property (weak, nonatomic) HorizontalLineView *horizontalLineView; // custom subclass of UIView that all it does is draw horizontal lines
@property (weak, nonatomic) UITextView *textView;
@end
@implementation NoteView
@synthesize horizontalLineView = _horizontalLineView;
@synthesize textView = _textView;
#define LEFT_MARGIN 20
- (void)setup
{
// Create the subviews and set the frames
self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
CGRect textViewFrame = CGRectMake(LEFT_MARGIN, 0, self.frame.size.width, self.frame.size.height);
self.textView = [[UITextView alloc] initWithFrame:textViewFrame];
// some addition setup stuff that I didn't include in this question...
// Finally, add the subviews to the view
[self addSubview:self.textView];
[self insertSubview:self.horizontalLineView atIndex:0];
}
- (void)awakeFromNib
{
[super awakeFromNib];
[self setup];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
Here’s a line of your code:
Recall that the
horizontalLineViewproperty is weak. Let’s walk through what really happens in that line, with the extra code that ARC generates. First, you send theallocandinitWithFrame:methods, getting back a strong reference:At this point, the
HorizontalLineViewobject has a retain count of 1. Next, because you used dot-syntax to set thehorizontalLineViewproperty, the compiler generates code to send thesetHorizontalLineView:method toself, passingtempas the parameter. Since theHorizontalLineViewproperty is declaredweak, the setter method does this:That sets
self->_horizontalLineViewequal totemp, and puts&self->_horizontalLineViewon the object’s list of weak references. But it does not increment the retain count of theHorizontalLineViewobject.Finally, because the
tempvariable is no longer needed, the compiler generates this:That lowers the
HorizontalLineViewobject’s retain count to zero, so it deallocates the object. During deallocation, it walks down the list of weak references, and sets each one tonil. Soself->_horizontalLineViewbecomesnil.The way to fix this is to make the
tempvariable explicit, so that you can extend its lifetime until after you have added theHorizontalLineViewobject to its superview, which retains it: