0

In one of the iPad Application, I am working I have added custom views to a view. This works fine but now I want to remove all the custom views added. How do I do that?

Following is my code for adding custom views

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++;
}
4

3 に答える 3

4

親ビューからタイプ ComponentCustomView のすべてのサブビューを削除できます

for (UIView *view in self.formConatinerView.subviews) {
    if ([view isKindOfClass:[ComponentCustomView class]) {
        [view removeFromSuperview];
    }
}
于 2012-06-27T10:05:27.660 に答える
0

反復される配列 (この場合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]; 
于 2012-06-27T10:21:16.027 に答える
0
NSArray  *arr = [self.view subViews];

for (UIView *view in arr) {
    if ([view isKindOfClass:[ComponentCustomView class]) {
        [view removeFromSuperview];
    }
}
于 2012-06-27T10:14:17.953 に答える