【发布时间】:2012-03-17 03:32:24
【问题描述】:
我正在向我的视图添加两个由属性存储的子视图。将子视图添加到我的视图时,子视图似乎在我的 setup 方法被调用后被释放。最终结果是视图永远不会显示。现在,如果我将我的属性更改为strong 而不是weak,我会保留对视图的引用,它们现在会显示在屏幕上。那么这里发生了什么?为什么addSubview: 和insertSubview: 不保留子视图?请看下面的代码:
顺便说一句,我正在使用带有 ARC 的 iOS5(因此有强和弱的东西)
#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
标签: iphone objective-c ios cocoa-touch uiview