Removing subviews from a view.

In my apps, I use swipe gestures to move to the next screen. The old stuff is animated off to the left and the new stuff animates in from the right. If the user rotates the device, I need to remove the elements of the view from the main view and redisplay them in the new orientation. I also need to remove the animations if they are occurring. I have lots of different layouts, but each layout has this method in it. (The contents vary a bit.)

I first remove the animations. Then I remove the views from the main view. Then I set the views to nil to make sure their resources are released.


- (void)removeAllObjectsFromParentView {
    [self.wordView.layer removeAllAnimations];
    [self.categoryView.layer removeAllAnimations];
    
    [self.wordView removeFromSuperview];
    [self.categoryView removeFromSuperview];
   
    self.wordView = nil;
    self.categoryView = nil;
}

All of the subviews of wordView and categoryView will be removed and released when the parent view is removed and released.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.