Last active
November 29, 2016 13:23
-
-
Save olxios/06c3c60c83bd19cb694b3a13a57144b2 to your computer and use it in GitHub Desktop.
Using NSNotificationCenter with UIView subclass: http://swiftiostutorials.com/quick-tip-adding-removing-nsnotificationcenter-observers-uiview-subclass/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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