0

私は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
4

1 に答える 1

3

以下のコード行を試してください。

for(UIView *viewInner in self.view.subviews) {
    if([viewInner isKindOfClass:[Post class]])
        [viewInner removeFromSuperview];
}
于 2012-12-17T13:36:11.200 に答える