我有這兩段代碼。第一個完美的作品:iPhone:[子視圖發佈]從顯示中刪除我的子視圖
UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 60.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:tmp];
[tmp release];
第二個是幾乎相同,但視圖不顯示。
CommentBox *commentBox = [[CommentBox alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:commentBox];
[commentBox release]; // Why does this remove the view?
如果我刪除了[commentBox release]
,則視圖出人意料地出現。但是我沒有看到這兩個代碼片段有所不同。
爲CommentBox初始化看起來是這樣的:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Load the nib:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CommentBox" owner:self options:nil];
self = [nibObjects objectAtIndex:0];
}
return self;
}
謝謝格雷厄姆。下面我發佈了我的新解決方案。 – znq 2010-05-19 10:13:03