Create a gist now

Instantly share code, notes, and snippets.

@implementation TestView
#pragma mark - Observers
- (void)didMoveToWindow
{
if (self.window) // If self.window != nil, it means view was added to the window
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(test:)
name:@"TestNotification"
object:nil];
}
}
- (void)willMoveToWindow:(UIWindow *)newWindow
{
if (newWindow == nil) // if newWindow == nil, it means view was removed from the window
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"TestNotification"
object:nil];
}
}
#pragma mark - Actions
- (void)test:(NSNotification *)notification
{
NSLog(@"Test!");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment