KexAri
KexAri

Reputation: 3977

Removing all of a subclassed UIView from self.view

I have a bunch of UIViews that I subclassed that I have been adding to self.view e.g.:

MySpecialView *myView = [[MySpecialView alloc] init];
[self.view addSubview:myView];

Now I want to remove them all from self.view but only those custom ones. I don't want to remove any of the others (I have some other views with options in them etc). Is there anyway of doing this at all? Can I loop through all the subviews and check their type? Any pointers on this would be great! Thanks!

Upvotes: 0

Views: 33

Answers (2)

anders
anders

Reputation: 4218

Swift way

    for subview in self.view.subviews {
        if subview.isKindOfClass(MyClass) {
            // Is that class!
        } else if subview.isMemberOfClass(MyClass) {
            // Is that class or a subclass of that class!
        }
    }

Upvotes: 0

Chris Loonam
Chris Loonam

Reputation: 5745

Try a loop like this

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

This simply iterates through all of the subviews and removes any that are of class MySpecialView.

Upvotes: 4

Related Questions