- (id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(echoNotification:) name:@"showSomething" object:nil];
}
return self;
}
只要有 NSNotificationCenter 發佈有關 showSomething 訊息事件的通知,此物件就會執行 echoNotification: 函式。
- (void)echoNotification:(NSNotification *)notification {
//取得由NSNotificationCenter送來的訊息, 假設為 NSArray 形態的參數。
NSArray *anyArray = [notification object];
NSLog(@"%@", [anyArray componentsJoinedByString:@"\n"]);
}
取消訂閱
[[NSNotificationCenter defaultCenter] removeObserver:self];
對特定的訊息事件取消訂閱
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"showSomething" object:nil];
訊息通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"showSomething" object:stringArray];
沒有留言:
張貼留言