我有一個類調用Post和它的視圖....我在我的主視圖中添加了一些像這樣的東西: 鎖定如何移除和釋放此對象我self.view .... 我只是有這個類:從視圖中移除並釋放一個類型對象
@interface Post : UIView{
UILabel * label1;
UILabel * label2;
UILabel * label3;
}
@implementation Post
-(void)init
{
self = [super init];
if (self) {
label1 = [[UILabel alloc]init];
label2 = [[UILabel alloc]init];
label3 = [[UILabel alloc]init];
label1.frame = CGRect(10,10,100,30);
label2.frame = CGRect(10,60,100,30);
label3.frame = CGRect(10,110,100,30);
}
return self;
}
@end
這個主類控制器
@implementation HomeViewController
-(void)viewDidLoad{
Post *p = [[Post alloc]init]
p.frame = CGRect(0,0,0,320,460);
p.backgroundColor = [UIColor blue];
[self.view addsubview: p];
[p release];
Post *p2 = [[Post alloc]init]
p2.frame = CGRect(0,0,0,320,460);
p2.backgroundColor = [UIColor blue];
[self.view addsubview:p2];
[p2 release];
Post *p3 = [[Post alloc]init]
p3.frame = CGRect(0,0,0,320,460);
p3.backgroundColor = [UIColor blue];
[self.view addsubview:p3];
[p3 release];
}
-(void)RemoveAllPosts
{
//How to remove and release all post from self.view???
}
@end
是否包含' - (void)dealloc {label1 release]; [label2 release]; [label3 release]; }'Post'類中的'方法和Paramasivan已經回答了您的問題 –
ohhh ok !!如果我釋放我的自我釋放我的對象後,我不需要在dealloc釋放權?看起來像這樣:[p.label1 release]; [p.label2發佈]; [p.label3發佈]; [p釋放]; –
你可以做那個怪異的編碼,但dealloc是最好的地方釋放你分開的對象 –