Ali Hassan
Ali Hassan

Reputation: 519

Remove all objects from a UIView

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?

Upvotes: 7

Views: 4492

Answers (2)

Parvendra Singh
Parvendra Singh

Reputation: 985

Use This For remove all object from a UIView

  NSArray *arr=view.subviews; //Your Vies's object.subviews

            for (UIView *view1 in arr)
            {
                [view1 removeFromSuperview];

            }

Upvotes: 0

skytz
skytz

Reputation: 2201

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

[self.contentView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];

Upvotes: 17

Related Questions