一、概念
通知中心NSNotificationCenter,也就是广播,和java类似。消息发送者通过通知中心将消息发送给消息接受者。通知中心是一个单例。
相关的二个类:
NSNotificationCenter:消息中心,负责消息的发送和消息的接收,是单例。
NSNotification:消息,可以包含消息的各种信息。
二、消息的发送和消息的接收
1.消息类型
通知中心发送的消息是一个类NSNotification的对象,一个消息对象包含的内容:
@interface NSNotification: NSObject<NSCopying, NSCoding> -(NSString *) name;//获得消息的名字 -(id)object; //消息的对象 -(NSDictionary *)userinfo;//消息的参数 @end2.发送消息
发送消息有三种方法:
-(void)postNotification: (NSNotification *)notification; -(void)postNotificationName:(NSString *)aName object:(id)anObject; -(void)postNtificationName:(NSString *)aName object:(id)anObject userinfo: (NSDictionary *)aUserinfo;
3.消息的接收
-(void)addObserver: (id)observer selector:(SEL)aSekector name:(NSString *)aName object:(id)anObject;消息的接收是一种回调机制,接收消息需要先于发送消息之前注册,在发送消息之前,我们需要先注册广播接受者,这样发送的消息才能被接收到。
三、通知中心使用示例
1.创建BJBroadcast类,在该类中执行发送广播的操作。
BJBroadcast.h
#import <Foundation/Foundation.h> @interface BJBroadcast : NSObject //发送广播 -(void)broadcast; //循环发送广播 -(void)broadcastLooper; @end
#import "BJBroadcast.h" @implementation BJBroadcast //发送广播 -(void)broadcast{ //1.取得通知中心 NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; //2.编辑消息参数 static int i; NSString * count = [NSString stringWithFormat:@"broadcast %d",i++]; NSDictionary * dict= [NSDictionary dictionaryWithObjectsAndKeys:@"beijing broadcast", @"name", count, @"time", nil]; //3.发送广播 //形式1: // NSNotification * notif = [[NSNotification alloc] initWithName:@"BJBroadcast" object:self userInfo:dict]; // [center postNotification:notif]; //形式2: //[center postNotificationName:@"BJBroadcast" object:self]; //形式3: [center postNotificationName:@"BJBroadcast" object:self userInfo:dict]; } //设置一个定时器,循环发送广播 -(void)broadcastLooper{ [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(broadcast) userInfo:nil repeats:YES]; /* 参数1:间隔时长 参数2:目标 参数3:执行的代码 参数4:用户信息 参数5:是否循环 */ } @end2.创建AListener类,在该类中执行接收广播的操作
AListener.h
#import <Foundation/Foundation.h> @interface AListener : NSObject -(void)wantToListen; @end
#import "AListener.h" @implementation AListener //接收广播信息 -(void)receiveBroadcast: (NSNotification *)notification{ NSString * name = notification.name; NSLog(@"%@", notification); NSLog(@"广播名称:%@", name); } //监听器,监听广播 -(void)wantToListen{ NSNotificationCenter * center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(receiveBroadcast:) name:@"BJBroadcast" object:nil]; //只要有广播,就调用[self receiveBroadcast] //因为这里,广播的接收会有参数,所以selector的方法为receiveBroadcast: } @end3.在main函数中调用接收广播的函数和发送广播的函数
#import <Foundation/Foundation.h> #import "BJBroadcast.h" #import "AListener.h" int main(int argc, const char * argv[]) { @autoreleasepool { BJBroadcast * bjcast = [[BJBroadcast alloc] init]; AListener * listener = [[AListener alloc] init]; //先注册监听器 [listener wantToListen]; //然后发送广播。必须先注册监听器再发送广播才能接收到消息。 //发一次广播 //[bjcast broadcast]; //循环发广播 [bjcast broadcastLooper]; //使得线程表现得更好,有事件时唤醒线程,没事件时休眠。 [[NSRunLoop currentRunLoop] run]; } return 0; }广播是非常有用的一种设计模式,比如在开发app一款购物类app时,我们需要再多个页面都能即时的修改购物车的产品数量,这时可以使用该机制,在购物车类中注册广播接受者,接收其它页面发送的关于购物车产品数量的消息,实时修改购物车产品的数目。