现在的位置: 首页 > 综合 > 正文

学习NSNotification经历

2012年10月05日 ⁄ 综合 ⁄ 共 9719字 ⁄ 字号 评论关闭

A   17:02:32
我看到例子
    if (motion == UIEventSubtypeMotionShake )
    {
        // User was shaking the device. Post a notification named "shake".
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
    }
A   17:02:40
请问这样有什么效果吗?
A   17:03:07
只是post 其他什么都没做
B   17:04:14
addObserver:selector:name:object:  你在程序里搜下这个函数
A   17:04:27
看到了
B   17:04:49
看name:的参数是不是shake
A   17:05:22
对啊  这不是这个notification的名字吗?
B   17:05:20
他这样的写法是传递不了参数的
A   17:05:44
哦 那要如何传?

B   17:05:47
- (void)postNotification:(NSNotification *)notification  用这个传递
B   17:06:04
自己先定义一个nsnotification 对象
B   17:06:25
+ (id)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)userInfo   
B   17:06:49
userInfo就是你要传递的参数
B   17:06:55
addObserver:selector:name:object:
A   17:06:56
在userinfo里传递?
B   17:07:24

A   17:07:25
先定义再监听  就可以获取了?
B   17:07:51
addObserver:selector:name:object:  的selector应该是这样子的
A  17:08:40
那这样selector 可不可以不用调用函数了?
B   17:08:41
-(void)foo:(nsnotification *)notification {
     id yourParam = [notification userInfo];

B   17:09:00
selector 就调用foo
B   17:09:43
nsnotificationcenter调用的foo
A   17:10:55
然后获取的时候就  [notification userinfo]?
B   17:11:54
[NSNotificationCenter postNotification:[NSNotification notificationWithName:"Shake" object:nil userInfo:(your param)]]
B   17:12:04

A   17:12:21
恩 大概了解了  太感谢了 纠结一天了 
B   17:12:42
这是一个观察者模式
A   17:13:22
第一次接触 搞不懂

A   17:13:36
确实不懂这个notification的作用
A  17:14:24
还有个问题 
B  17:14:26
NSNotification 用来保持事件名和你需要传递的参数
A   17:14:50
如果不传递参数 那notification 不只起到seletor的作用吗/
A   17:15:02
对于post方  有什么作用?
A   17:15:12
就保持事件名吗?
B   17:16:01
嗯   对,  如果不传递参数nsnotification用处不大
A   17:16:34
疑团 慢慢解开了  舒畅啊 
A   17:16:38
哈哈 谢谢了

(A 我,小白     B 大牛)

----后记,补充
经过2天左右的时间,终于把NSNotification的问题搞定了,太开心了。
昨天做了一个demo,一直不成功,心情很不爽,今天来了继续向大牛请假,终于首先把NSNotification的原理搞懂了。
postNotification的作用是保存参数,并把这个新建立的notification发送给NotificationCenter,NotificationCenter的作用就是消息的存储库,既可以向里面存放消息,又可以从里面提取消息。
当用post传递参数之后,在其他类中用 [[NSNotificationCenter defaultCenter] addObservet:self selector:@selector(method) name:@"string" object:nil]; 对消息进行监听,要确保string和你之前存放时的notification名字相同,method的作用就是提取参数。
-(void)foo:(NSNotification *)notification
{
    NSDictionary *sendValue=[notification userInfo];
    scoreLabel.text=[[sendValue objectForKey:@"scoreValue"] stringValue];
}
这是我例子中的method 。 到此基本理清了,也应该会运用了。

有收获的感觉真好。

 使用 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
使用 Notifications" title="第十四章: 使用 Notifications" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; vertical-align: middle; border-style: initial; border-color: initial; max-width: 500px; " />
. 如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
. 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer
- (void)postNotification:(NSNotification *)notification
发送notification至notification center 如图14.2
使用 Notifications" title="第十四章: 使用 Notifications" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; display: inline; vertical-align: middle; border-style: initial; border-color: initial; max-width: 500px; " />
- (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声音

简单说,
1. 效率肯定是delegate比nsnotification高。

2. delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值,也就是delegate方法的结果。比如-windowShouldClose:,需要关心返回的是yes还是no。所以delegate方法往往包含should这个很传神的词。也就是好比你做我的delegate,我会问你我想关闭窗口你愿意吗?你需要给我一个答案,我根据你的答案来决定如何做下一步。相反的,notification最大的特色就是不关心接受者的态度,我只管把通告放出来,你接受不接受就是你的事情,同时我也不关心结果。所以notification往往用did这个词汇,比如NSWindowDidResizeNotification,那么nswindow对象放出这个notification后就什么都不管了也不会等待接受者的反应。

如果2个模块之间有关系,就用delegate

比如,view 和 subview,view 和viewController,前一个view 和后一个view,等等兄弟姐妹父子关系

2个模块不沾边的情况下,选notification

抱歉!评论已关闭.