tags:

views:

561

answers:

2

When my app gets back to it's root controller, in the ViewDidAppear I need to remove all subviews.

How is this done?

+6  A: 

You can use the convenient mehtod makeObjectsPerformSelector: as follows:

NSArray * subviews = [self.view subviews];
[subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

Note: This will remove every view that your main view contains and release them, if they are not retained elsewhere. From Apple's documentation on removeFromSuperview:

If the receiver’s superview is not nil, this method releases the receiver. If you plan to reuse the view, be sure to retain it before calling this method and be sure to release it as appropriate when you are done with it or after adding it to another view hierarchy.

e.James
* should be "removeFromSuperview" not "...SuperView". The v in view should be lowercase.
Ward
Good eye. I have changed it. Thank you!
e.James
+4  A: 

Get all the subviews from your root controller and send each a removeFromSuperview:

NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
    [v removeFromSuperview];
}
Matthew McGoogan
I like e.James' method better.
Matthew McGoogan
+1 and thank you. I should have also used `self.view` as you have.
e.James