プロパティで保存された2つのサブビューを私のビューに追加します。私のビューにサブビューを追加すると、私のsetup
メソッドが呼び出された後、サブビューが一見外されてしまいます。最終的には、ビューは決して表示されません。今、weak
とは対照的に、プロパティをstrong
に変更すると、ビューへの参照が保持され、画面に表示されます。だからここで何が起こっているの? addSubview:
とinsertSubview:
にサブビューが保持されないのはなぜですか?私はなぜaddSubviewはビューを保持していませんか?
#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;
}
作成する方法NoteViewのインスタンス?コントロールがセットアップに到達しているのを参照してください。 –
私は、それをストーリーボードのビューコントローラに追加することによって、NoteViewのインスタンスを作成しています。 –
クラスがXibでNoteViewとして指定されています –