remove all UIButton's from subview

10,459

Solution 1

A more efficient way would be to add each button to an array when you create it, and then when a button is pressed, have all the buttons in the array call the -removeFromSuperView method like this:

[arrayOfButtons makeObjectsPerformSelector:@selector(removeFromSuperView)];

Then after that, you can either keep the buttons in the array and reuse them, or call removeAllObjects to have them released. Then you can start populating it again later.

This saves you from having to walk through the entire view hierarchy looking for buttons.

Solution 2

Another answer just for reference:

for (int i = [self.view.subviews count] -1; i>=0; i--) {
    if ([[self.view.subviews objectAtIndex:i] isKindOfClass:[UIButton class]]) {
        [[self.view.subviews objectAtIndex:i] removeFromSuperview];
    }
}

Solution 3

NSMutableArray *buttonsToRemove = [NSMutableArray array];
for (UIView *subview in self.view.subviews) {
    if ([subview isKindOfClass:[UIButton class]]) {
        [buttonsToRemove addObject:subview];
    }
}
[buttonsToRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];

EDIT:
I have edited my answer to a better solution.
Now the objects are not removed from the array while enumerating it...

Solution 4

Also try this it's very simple :

 for (UIButton *btn in self.view.subviews){     
              [btn removeFromSuperview]; //remove buttons
    }
Share:
10,459
Martijn
Author by

Martijn

Updated on June 05, 2022

Comments

  • Martijn
    Martijn almost 2 years

    i'm programmatically adding a couple UIButtons to my view. After clicking one of the buttons they all should be 'removeFromSuperView' or released, not just one.

    for (int p=0; p<[array count]; p++) {  
        button = [[UIButton alloc] initWithFrame:CGRectMake(100,100,44,44)];  
        button.tag = p;  
        [button setBackgroundImage:[UIImage imageNamed:@"image.png"]   forState:UIControlStateNormal];    
        [self.view addSubview:button];    
        [button addTarget:self action:@selector(action:)   forControlEvents:UIControlEventTouchUpInside];  
    }
    

    Now this is the part where all buttons should be removed. Not just one.

    -(void) action:(id)sender{  
        UIButton *button = (UIButton *)sender;  
        int pressed = button.tag;  
        [button removeFromSuperview];  
    }
    

    I hope someone can help me with this one!

  • Felix Lamouroux
    Felix Lamouroux about 14 years
    it should read "for (UIView *subview in self.view.subviews)" I guess
  • Martijn
    Martijn about 14 years
    Thnx Micheal! After changing (UIView *subview in self.view) in: (UIView *subview in [self.view subviews]) it works like a charm!
  • Michael Kessler
    Michael Kessler about 14 years
    @Felix, thank you for the correction. You are absolutely right. I've edited my answer.
  • choise
    choise about 14 years
    really clean idea. i did that always, with an extra view and then performing this "makeObjectsPerformSelector:" to that view. but doing this with an array is much better.
  • Stefan Arentz
    Stefan Arentz about 14 years
    I downvoted this answer because it is bad advice. Apple clearly states that while using Fast Enumeration you should not modify the collection's contents. See developer.apple.com/Mac/library/documentation/Cocoa/Conceptu‌​al/…
  • Daniel
    Daniel over 10 years
    as St3fan said, while using fast enumeration you SHOULD NOT modify the collection's content.