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
發送notification至notification center
- (void)postNotificationName:(NSString *)aName
object:(id)anObject
建立并發送一個notification
移除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];
}