2012-06-27 2 views
0

Dans l'une de l'application iPad, je travaille j'ai ajouté des vues personnalisées à une vue.Cela fonctionne bien, mais maintenant je veux supprimer toutes les vues personnalisées ajoutées.Comment puis-je faire cela?Comment supprimer des customviews ajoutés par mode de programmation?

Après mon code pour ajouter des vues personnalisées

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

Répondre

4

Vous pouvez supprimer tous les sous-vues de type ComponentCustomView de vos vues parent

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

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

Je ne suis pas sûr que la suppression d'objets à partir du tableau itéré (dans ce cas subviews) était sûr ou pas (je me souviens avoir lu quelque chose à propos d'un écart entre Mac OS X et iOS, mais pas sûr ...); À moins que la propriété subviews renvoie une copie du tableau interne (très probable puisque le tableau interne doit être mutable), 100% sûr, façon juste en cas de le faire serait:

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]; 
Questions connexes