Newbee
Newbee

Reputation: 3301

How to manage subviews added in a UIView?

I have a problem in clearing my view, For a particular case I have been adding user names as label into my view, I may have multiple user based on situation, now my problem is, For one case I want to clear the view without popping it, I am not sure how to remove those added labels, I have an idea I can set tag for each label, and I can clear using that later. Any other efficient way is there in this particular case.

Hope my question is clear thanks.

Upvotes: 0

Views: 327

Answers (7)

Nishant B
Nishant B

Reputation: 2907

Call below method and pass the "UIWebView" object as argument:

+(void)removeAllSubViews:(id)pObj
{
    NSArray *Array = [pObj subviews];
    for(int index = 0; index < [Array count]; index++)
    {
        [[Array objectAtIndex:index] removeFromSuperview];
    }
}

You can also check object like this if you want for any particular object:

if(view isKindOfClass:[UILabel class]) {
    //do whatever you want to do
}

Cheers!

Upvotes: 0

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

Try this:

for (UIView *v in [self.relatedView subviews])
{
    [v removeFromSuperview];
}

Upvotes: 0

Satish Azad
Satish Azad

Reputation: 2312

use this :

for(id viewSub in self.view.subviews)
    {
        [viewSub removeFromSuperview];
    }

this will remove all subviews of View

Upvotes: 0

user991278
user991278

Reputation:

[labelName removeFromSuperview];

Upvotes: 0

Exploring
Exploring

Reputation: 935

You can do in the following way

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

Hope you are asking about this.

Upvotes: 0

Nikita P
Nikita P

Reputation: 4246

use

for (UIView* view in self.view.subviews) {
    if(view isKindOfClass:[UILabel class]) {
        //do whatever you want to do
    }
}

Upvotes: 2

Andy
Andy

Reputation: 14302

You can remove all subviews like this:

for (UIView *subView in [view subviews])
    [subView removeFromSuperview];

Or if you want to access a particular view with tag value of n,

UIView *subview = [view viewWithTag:n]

Upvotes: 0

Related Questions