1、定义一个方法
-(void) update{ }
2、对象注册,并关连消息
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(update) name:@"update"object:nil]
3、在要发出通知消息的地方
[[NSNotificationCenter defaultCenter]
postNotificationName:@"update" object:nil];
具体如何使用 Notifications
http://blog.sina.com.cn/s/blog_5df7dcaf0100c0q2.html
什么是Notification?
用户可能使用RaiseMan并打开了几个document,然后他发现紫色的背景颜色实在是不利于阅读文档正文.
于是,他打开Preferencespanel修改背景颜色,不过令人失望的是,已经存在的文档的背景颜色不会跟着改变.
于是,这个用户可能会写信给你告诉你这些. 你也许会回复:”defualts会在document创建的时候才读取,保存document再打开”实际上,用户想说明的是他希望程序能立马刷新已经打开的文档.
如果这样,那该怎么做呢?
我们需要把所有打开的document用一个list记录起来么?
这个要求其实也很容易实现. 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏. 对象注册关注某个确定的notification
(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做 observer
. 其它的一些对象会给center
发送notifications
(我捡到了一只小狗). center
将该notifications
转发给所有注册对该notification
感兴趣的对象. 我们把这些发送notification
的对象叫做 poster
Notification对象非常简单. 它就是poster
要提供给observer
的信息包裹. notification
对象有两个重要的成员变量: name
和 object
. 一般object
都是指向poster
(为了让observer
在接受到notification
时可以回调到poster
)
所以,notification
有两个方法
- (NSString *)name
- (id)object
NSNotificaitonCernter是架构的大脑了.它允许我们注册observer
对象, 发送notification
, 撤销observer
对象注册
下面是它的一些常用方法
+ (NSNotificationCenter *)defaultCenter
返回notification center
[类方法,返回全局对象, 单件模式.cocoa的很多的全局对象都是通过类似方法实现]
- (void)addObserver:(id)anObserver
selector:(SEL)aSelector
name:(NSString *)notificationName
object:(id)anObject
注册anObserver
对象:接受名字为notificationName
, 发送者为anObject
的notification
. 当anObject
发送名字为notificationName
的notification
时, 将会调用anObserver
的aSelector
方法,参数为该notification
如果notificationName
为nil
. 那么notification center
将anObject
发送的所有notification
转发给observer
如果anObject
为nil
.那么notification center
将所有名字为notificationName
的notification
转发给observer
- (void)postNotification:(NSNotification *)notification
发送notification至notification center
- (void)postNotificationName:(NSString *)aName
object:(id)anObject
创建并发送一个notification
- (void)removeObserver:(id)observer
移除observer
发送一个Notification
发送notification是其中最简单的步骤了,所以我们从它开始实现.当我们接收到changeBackgroundColor:
消息时, PreferenceController
对象发送一个notification
.
我们将notification
命名为@"BNRColorChanged"
,我们使用一个全局常量来指定.(有经验的程序员会使用一个前缀,这样避免和其他组件定义的notification
混淆)打开PreferenceController.h添加下面的的外部申明
extern NSString * const BNRColorChangedNotification;
在PreferenceController.m中定义常量
NSString * const BNRColorChangedNotification = @"BNRColorChanged";
在PreferenceController.m修改changeBackgroundColor:
方法
- (IBAction)changeBackgroundColor:(id)sender
{
NSColor *color = [colorWell color];
NSData *colorAsData =
[NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorAsData
forKey:BNRTableBgColorKey];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSLog(@"Sending notification");
[nc postNotificationName:BNRColorChangedNotification object:self];
}
注册成为Observer
要注册一个observer
, 我们必须提供几个要数: 要成为observer
的对象;所感兴趣的notification
的名字;当notification
发送时要调用的方法. 我们也可以指定要关注莫个对象的notification
.(比如说,我们需要关注莫个特定的window
的resize
的notification
)
编辑MyDocument
类的init
方法
- (id)init
{
if (![super init])
return nil;
employees = [[NSMutableArray alloc] init];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:BNRColorChangedNotification
object:nil];
NSLog(@"Registered with notification center");
return self;
}
同时在dealloc方法,将MyDocument从notification center中移除
- (void)dealloc
{
[self setEmployees:nil];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self];
[super dealloc];
}
处理Notification
当一个notification
发生时, handleColorChange:
方法将被调用. 目前我们在方法中简单的打印一些log.
- (void)handleColorChange:(NSNotification *)note
{
NSLog(@"Received notification: %@", note);
}
编译运行程序,看到了我们想要的log了吧
userInfo Dictionary
notification
对象的object
变量是poster
,如果我们想要notification
对象传递更多的信息,我们可以使用user info dictionary.
每个notification
对象有一个变量叫 userInfo
, 它是一个NSDictionary对象,用来存放用户希望随着notification
一起传递到observer
的其它信息. MyDocument将使用它来得到要改变的color
.在PreferenceController.m添加userInfo
- (IBAction)changeBackgroundColor:(id)sender
{
NSColor *color = [sender color];
NSData *colorAsData;
colorAsData = [NSKeyedArchiver archivedDataWithRootObject:color];
[[NSUserDefaults standardUserDefaults] setObject:colorAsData
forKey:BNRTableBgColorKey];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSLog(@"Sending notification");
NSDictionary *d = [NSDictionary dictionaryWithObject:color
forKey:@"color"];
[nc postNotificationName:BNRColorChangedNotification
object:self
userInfo:d];
}
在MyDocument.m,从userInfo
中读取到color
- (void)handleColorChange:(NSNotification *)note
{
NSLog(@"Received notification: %@", note);
NSColor *color = [[note userInfo] objectForKey:@"color"];
[tableView setBackgroundColor:color];
}