2012-12-17 4 views
0

클래스 게시와 해당보기가 있습니다. 내 기본보기에서 다음과 같이 몇 번 추가합니다. 잠그기이 객체를 제거하고 해제하는 방법보기에서 유형 객체를 제거하고 놓습니다.

@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 
+0

'- (void) dealloc { [label1 release]; [label2 release]; [label3 release]; }'Post' 클래스의 Paramasivan이 질문에 대답했습니다 –

+0

오오오! 릴리스 전에 내 자신에 의해 내 개체 게시 내가 dealloc 오른쪽에서 릴리스 할 필요가 없었어요? 다음과 같이 보입니다 : [p.label1 release]; [p.label2 release]; [p.label3 release]; [피 릴리스]; –

+0

이상한 코딩을 할 수 있지만 dealloc은 할당 된 객체를 해제하는 가장 좋은 장소입니다 –

답변

3

코드 라인 아래에 시도하십시오 : 내 self.view .... 난 그냥이 클래스가 있습니다.

for(UIView *viewInner in self.view.subviews) { 
    if([viewInner isKindOfClass:[Post class]]) 
     [viewInner removeFromSuperview]; 
} 
+0

잘 작동합니다 ...이 viewInner를 릴리스해야합니까? –

+0

하위 뷰에 추가 한 후에 릴리스 명령을 추가하여 이미 릴리스했습니다. superview에서 제거하면 충분할 것입니다. –

+0

그게 바로 내가 생각하는 오! 내가 충돌을 풀려고하면 –

관련 문제