Я добавляю к моему представлению два подпредставления, которые хранятся в свойствах. При добавлении подпредставлений в мое представление подпредставления, по-видимому, освобождаются после вызова моего метода setup
. Конечным результатом является то, что представления никогда не отображаются. Теперь, если я изменю свои свойства на strong
вместо weak
, я сохраняю ссылку на представления, и теперь они отображаются на экране. Так что же здесь происходит? Почему addSubview:
и insertSubview:
не сохраняют подпредставления? См. код ниже:
кстати, я использую iOS5 с ARC (отсюда сильные и слабые стороны)
#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;
}