NSNotificationCenter 的详细说明

  1. 定义一个方法html

      -(void) update{       } 程序员

2. 对象注册,并关连消息架构

     [[NSNotificationCenter defaultCenter] 并发

     addObserver:self selector:@selector(update) name:@"update" object:nil]  app

3. 在要发出通知消息的地方ide

     [[NSNotificationCenter defaultCenter] 函数

     postNotificationName:@"update" object:nil];post

具体如何使用 Notifications设计

http://blog.sina.com.cn/s/blog_5df7dcaf0100c0q2.htmlserver

////////////////////////////////////////

第十四章: 使用 Notifications

用户可能使用RaiseMan并打开了几个document, 而后他发现紫色的背景颜色实在是不利于阅读文档正文. 

因而,他打开Preferences panel修改背景颜色,不过使人失望的是,已经存在的文档的背景颜色不会跟着改变. 

因而,这个用户可能会写信给你告诉你这些. 你也许会回复:"defualts会在document建立的时候才读取,

保存document在打开"实际上,用户想说明的是他但愿程序能立马刷新已经打开的文档. 若是这样,那该怎么作呢?

咱们须要把全部打开的document用一个list记录起来么?

--- 什么是Notification? ---

这个要求其实也很容易实现. 每一个运行中的application都有一个NSNotificationCenter的成员变量,

它的功能就相似公共栏. 对象注册关注某个肯定的notification(若是有人捡到一只小狗,就去告诉我). 

咱们把这些注册对象叫作 observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). 

center将该notifications转发给全部注册对该notification感兴趣的对象. 咱们把这些发送notification的对象叫作 poster

不少的标准Cocoa类会发送notifications: 在改变size的时候,Window会发送notification; 

选择table view中的一行时,table view会发送notification;咱们能够在在线帮助文档中查看到标准cocoa对象发送的notification

在咱们的例子中,咱们将MyDocumet对象注册为observer. 而preference controller在用户改变color时将发送notification. 

MyDocument在接受到该notification后改变background color

在MyDocument对象释放前,咱们必须从notification center移除咱们注册的observer. 通常咱们在dealloc方法中作这件事

-- Notifications 不是什么 --

当程序员们听到notification center的时候, 他们可能会联想到IPC(进程间通信).他们认为:

"我在一个程序中建立一个observer,而后在另一个程序中发送一个notification". 这个设计没有办法工做的, 

notification center容许同一个程序中的不一样对象通许,它不能跨越不一样的程序 [Notification 就是设计模 式中的 观察者模式, 

cocoa为咱们实现了该模式, 就像Java也有一样的实现同样]

 

-- NSNotification 和 NSNotificationCenter

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 如图14.1

. 若是notificationName为nil. 那么notification center将anObject发送的全部notification转发给observer

. 若是anObject为nil.那么notification center将全部名字为notificationName的notification转发给observer

 

- (void)postNotification:(NSNotification *)notification

发送notification至notification center 如图14.2

 

- (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];

}

打开几个窗口,并改变背景颜色,如今,那些打开的窗口的背景颜色立马就变了.

-- 思考 --

一般当你将本身的一个对象设置为cocoa某个标准对象的delegate的时候,你同时或许也对该标准对象的notification感兴趣. 

例如,咱们实现一个window的delegate来处理 windowShouldClose: , 咱们也许会对 NSWindowDidResizeNotification

这样的notification感兴趣.

若是一个cocoa标准对象有一个delegate,同时它也发送notification的话, cocoa对象会自动将它的delegate对象注册

成为observer来接受接受本身的notification. 若是咱们实现了一个delegate,那么delegate[也就是咱们的对象]

要怎样声明来接受notification呢?[方法的名字是什么?]

 

方法名字其实很简单: 以notification名字为基准, 先将NS前缀去掉,接着将第一个字母改成小写. 在将后面的Notification去掉,

而后加个冒号:. 例如,为了能接受到window的NSWindowDidResizeNotification, delegate能够实现方法:

- (void)windowDidResize:(NSNotification *)aNotification

 

当window改变大小时,这个方法将自动调用. 对于NSWindow,咱们能够在.h或是帮助文档中找到相似的notification 

来实现notification方法.

-- 挑战 --

当程序再也不是active状态是,让程序发出beep. 当unactive时,NSApplication会发送

NSApplicationDidResignActiveNotification的notificaiton. 而咱们的AppController是NSApplication的delegate.   

函数NSBeep()能够用来发出beep声音