Last active
January 5, 2018 03:56
-
-
Save vitonzhangtt/801e7f52eb09b458c7e88c81790008aa to your computer and use it in GitHub Desktop.
NSNotification
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
#import <UIKit/UIKit.h> | |
#import "AppDelegate.h" | |
#import "TTUserObject.h" | |
int main(int argc, char * argv[]) { | |
@autoreleasepool { | |
TTUserObject *userObject = [[TTUserObject alloc] init]; | |
[userObject postNicknameDidChangedNotification]; | |
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); | |
} | |
} | |
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
#import <Foundation/Foundation.h> | |
@interface TTUserObject : NSObject | |
- (void)postNicknameDidChangedNotification; | |
@end |
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
#import "TTUserObject.h" | |
static NSString * const kUserDidChangeNicknameNotification = @"kUserDidChangeNicknameNotification"; | |
@implementation TTUserObject | |
- (instancetype)init { | |
if (self = [super init]) { | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(onUserDidChangeNickname:) | |
name:kUserDidChangeNicknameNotification | |
object:nil]; | |
} | |
return self; | |
} | |
- (void)postNicknameDidChangedNotification { | |
NSLog(@"A"); | |
[[NSNotificationCenter defaultCenter] postNotificationName:kUserDidChangeNicknameNotification | |
object:nil]; | |
NSLog(@"C"); | |
} | |
- (void)onUserDidChangeNickname:(NSNotification *)notification { | |
NSLog(@"B"); | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment