标签:
// Person.m 文件
@implementation Person
- (void)newsCome:(NSNotification *)note{
Company *obj = note.object;
NSLog(@"\n公司名: %@ -- 新闻名: %@ -- 接收者: %@ -- 新闻类型: %@", obj.name, note.name, self.name, note.userInfo);
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
// main.m 文件
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc] init];
p.name = @"XingXing";
Person *p2 = [[Person alloc] init];
p2.name = @"xiaoming";
Company *cy = [[Company alloc] init];
cy.name = @"sina";
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:p selector:@selector(newsCome:) name:@"news" object:cy];
[center addObserver:p2 selector:@selector(newsCome:) name:nil object:nil];
[center postNotificationName:@"news" object:cy userInfo:
@{@"city" : @"Shanghai",
@"program" : @"ios"}
];
return 0;
}
标签:
原文地址:http://www.cnblogs.com/lianfu/p/4868418.html