2012-06-27 2 views
0

iPad 응용 프로그램 중 하나에서보기에 사용자 정의보기를 추가했습니다.이 작업은 정상적으로 수행되었지만 지금은 모든 사용자 정의보기를 제거하고 싶습니다. 어떻게합니까? 다음프로그래밍 방식으로 추가 된 customviews를 제거하는 방법은 무엇입니까?

for (UIView *view in self.formConatinerView.subviews) { 
    if ([view isKindOfClass:[ComponentCustomView class]) { 
     [view removeFromSuperview]; 
    } 
} 

답변

4

를 추가하는 내 코드입니다 (이 경우 subviews) 반복되는 배열로부터 안전하지 않습니까? (Mac OS X과 iOS의 불일치에 대해 읽었지만 확실하지는 않습니다.); 속성 subviews이 내부 배열의 사본을 반환하지 않는 한 (내부 배열이 변경 가능해야 할 가능성이 높기 때문에) 100 % 안전하고 단순한 방법으로 다음과 같이 할 수 있습니다.

NSArray* copyOfSubviews = [[NSMutableArray alloc] initWithArray:[myView subviews]]; 
// Explicitly made mutable in an attempt to prevent Cocoa from returning 
// the same array, instead of making a copy. Another, tedious option would 
// be to create an empty mutable array and add the elements in subviews one by one. 

for(UIView* view in copyOfSubviews){ 
    if ([view isKindOfClass:[ComponentCustomView class]){ 
     [view removeFromSuperview]; 
    } 
} 

// (This is for non-ARC only:) 
[copyOfSubviews release]; 
0
NSArray *arr = [self.view subViews]; 

for (UIView *view in arr) { 
    if ([view isKindOfClass:[ComponentCustomView class]) { 
     [view removeFromSuperview]; 
    } 
} 
0

내가 제거 객체 있는지 확실하지 않습니다 당신은 당신의 부모 뷰에서 형 ComponentCustomView의 모든 서브 뷰를 제거 할 수 있습니다 사용자 정의보기

for (int col=0; col<colsInRow; col++) { 
     // NSLog(@"Column Number is%d",col); 
     x=gapMargin+col*width+gapH*col; 


     //self.styleButton=[[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
     ComponentCustomView *componentCustomobject=[[ComponentCustomView alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
     componentCustomobject.backgroundColor=[UIColor redColor]; 
     componentCustomobject.componentLabel.text=[appDelegate.componentsArray objectAtIndex:row]; 
     [self.formConatinerView addSubview:componentCustomobject]; 
     tempCount1++; 
    } 
관련 문제