Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be notified when a UIView detached from its superView?

Tags:

It seems that the UIView has not methods like "didRemoveFromSuperview" or "willRemoveFromSuperview".Then,How to listen to the event when a UIView removed from its superView?I should use KVO? thanks in advance!

like image 820
Jagie Avatar asked Jun 19 '10 08:06

Jagie


1 Answers

This works (tested on iOS8):

-(void) didMoveToWindow {
    [super didMoveToWindow]; // (does nothing by default)
    if (self.window == nil) {
        // YOUR CODE FOR WHEN UIVIEW IS REMOVED
    }
}

According to the UIView docs:

The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the window changes.

The window property may be nil... This occurs when the receiver has just been removed from its superview or when the receiver has just been added to a superview that is not attached to a window.

like image 127
MarkWPiper Avatar answered Nov 24 '22 00:11

MarkWPiper