Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all objects from a UIView

Tags:

ios

iphone

I am using 2 UIView to draw different objects in a xib file. There is a need to clear the view before drawing new objects for some actions. Initially,when object types are smaller in number, I have been using this:

for (UILabel *btn in self.contentView.subviews)
{            
    if([btn isKindOfClass:[UILabel class]])
    {
        [btn removeFromSuperview];
    }        
}

But when I have multiple actions and objects of multiple types are to be drawn for each action, it looks bad coding to use this type of method. Is there some efficient method to do this?

like image 833
Ali Hassan Avatar asked Nov 27 '13 06:11

Ali Hassan


People also ask

How do I remove all Subviews from UIView?

For UIView, you can safely use makeObjectsPerformSelector: because the subviews property will return a copy of the array of subviews. Using makeObjectsPerformSelector method you can remove all the subviews but, not subviews based on conditions.

How do I delete all arranged Subviews?

Managing Subviews To remove an arranged subview that you no longer want around, you need to call removeFromSuperview() on it. The stack view will automatically remove it from the arranged subview list.

How do I delete all Subviews in Swift?

I found this was because the self. subviews was mutated (maybe) when the removeFromSuperview() was called. By doing . copy(), I could perform the removal of each subview while mutating the self.


1 Answers

You should use this to remove all subviews, regardless of their class.

[self.contentView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
like image 165
skytz Avatar answered Oct 14 '22 09:10

skytz