Skip to content

Instantly share code, notes, and snippets.

@vitonzhangtt
Last active January 5, 2018 03:56
Show Gist options
  • Save vitonzhangtt/801e7f52eb09b458c7e88c81790008aa to your computer and use it in GitHub Desktop.
Save vitonzhangtt/801e7f52eb09b458c7e88c81790008aa to your computer and use it in GitHub Desktop.
NSNotification
#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]));
}
}
#import <Foundation/Foundation.h>
@interface TTUserObject : NSObject
- (void)postNicknameDidChangedNotification;
@end
#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