1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @implementation Boss -(void)sendNotification { //发送通知 NSDictionary *message = @{@"notification" : @"contacts every one!"}; //创建通知对象(老板登陆boss账号) NSNotification * notification = [NSNotification notificationWithName:@"fky" object:self userInfo:message]; //向通知中心发送消息(发布消息) [[NSNotificationCenter defaultCenter] postNotification:notification]; } @end |
注意:notificationWithName 需要和addObserver 的name 一样
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@implementation
HumanResources
-
(instancetype)init
{
self
=
[super
init];
if (self)
{
//注册监听
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(doSomething:)
name:@"fky" object:nil];
}
return self;
}
-(void)doSomething:(NSNotification
*)notification
{
//处理的方法
NSDictionary
*rec=
[notification
userInfo];
//输出收到的信息
NSLog(@"收到广播:%@",
rec[@"notification"]);
}
@end
|