NSNotificationCenter 的详细说明
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
////////////////////////////////////////
第十四章: 使用 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声音
NSNotificationCenter 的详细说明的更多相关文章
- 对iOS10新增Api的详细探究
本文主要是一些对iOS新功能的探索,之前发现博客里关于iOS新功能的分析大多是过于概括,每个功能几句话,无法了解到具体的功能.所以本次的探索是基于Api层面,着重看一些具体用法所做的笔记,本来想分别画 ...
- iOS NSNotificationCenter详解
通知中心的特点: 1:同步执行 2: 一对多发送消息 3: 降低程序耦合度 通知中心是单例,目的就是从任意一个发送消息到任意一个接收者,是同步执行的. 那么什么是同步呢? 用网上经典的说法,就是我叫朋 ...
- 用一个案列详细讲解UITextFiled
一. 登陆界面的搭建 首先涉及到登录界面状态栏颜色的问题,我们需要将状态栏颜色改为白色,可以在控制器内实现方法更改 - (UIStatusBarStyle)preferredStatusBarStyl ...
- iOS8发展~Swift(三)UI详细解释
一个.总结 使用Swift去完成iOS的UI接口,事实上,目前的想法和OC实现几乎一致,只是在措辞非常大的差异,修改前更更大的个人控制.为有纯代码强迫症,所以接下来创建一个纯代码动项目,然后在此基础上 ...
- 苹果APNS在app中的详细实现
鉴于server稳定的开发难度非常大,小团队不建议自己开发.建议使用稳定的第三方推送方案,如个推,蝴蝶等. 要想使用苹果APNS推送消息,首先要把开发app的xcode所用证书上传到server上,当 ...
- Reveal详细安装教程
Reveal的详细安装使用 标签: Reveal 工具 调试 iOS 一.终端的操作 首先最重要的一点,要先把Reveal软件放到Application中,否则路径是错的,后面的设置也就没有作用了 打 ...
- ZIP压缩算法详细分析及解压实例解释
最近自己实现了一个ZIP压缩数据的解压程序,觉得有必要把ZIP压缩格式进行一下详细总结,数据压缩是一门通信原理和计算机科学都会涉及到的学科,在通信原理中,一般称为信源编码,在计算机科学里,一般称为数据 ...
- SASS教程sass超详细教程
SASS安装及使用(sass教程.详细教程) 采用SASS开发CSS,可以提高开发效率. SASS建立在Ruby的基础之上,所以得先安装Ruby. Ruby的安装: 安装 rubyinstaller- ...
- 史上最详细git教程
题外话 虽然这个标题很惊悚,不过还是把你骗进来了,哈哈-各位看官不要着急,耐心往下看 Git是什么 Git是目前世界上最先进的分布式版本控制系统. SVN与Git的最主要的区别 SVN是集中式版本控制 ...
随机推荐
- Java 线程综述
线程重在 线程同步和线程通信的编程 1.线程与进程? 线程是指程序在执行过程中,能够执行程序代码的一个执行单元.线程的状态:运行.就绪.挂起(suspend).结束; 进程是指一段正在执行的程序. ...
- C++实现对树的创建和前中后序遍历
#include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...
- VMware vSphere Client5.0与 Windows8不再有问题,解决VMware 5.0 客户端提示VMRC控制台的连接已断开
问题:VMware 5.0 客户端提示VMRC控制台的连接已断开...正在尝试重新连接,系统是win8的 网上解决办法: WIN8,在安装vmware vsphere client 5.0时出现兼容性 ...
- uva 1220
1220 - Party at Hali-Bula Time limit: 3.000 seconds Dear Contestant, I'm going to have a party at my ...
- Oracle知识整理
1.自带三种登录方式: Scott/tiger sys/manager system/manager 2.基本的操作 1) 建数据库 create tablespace 表空间的名称 dat ...
- C#基础学习文章导航
第一部分:入个门 C#入门篇-1:HelloWorld的类 C#入门篇-2:什么是变量 C#入门篇-3:数据类型及转换 C#入门篇-4:使用运算符 第二部分:流程控制语句 C#入门篇5-1:流程控制语 ...
- su和su -和sudo
1.su和sudo没有切换工作目录和环境变量,只是赋予用户权限, 而su -是真正切换到root登录,工作目录切换到/root,环境变量也同时改变. [root@oc3408554812 home]# ...
- spring aop编程
1.AOP,面向切面编程(aspect Oriental programing),使用aop,可以将处理切面aspect的代码注入到主程序,通常主程序的主要目的不是处理这些切面aspect,可以防止代 ...
- 分析与提取QQ木马盗号技术
程序大致的流程如下图: 因为是用画图工具画的,所以大家就将就看下把,有什么不对的地方请多多指教: 程序是用Delphi写的,只有加载器加了个upx壳,其他的都没有加壳:所以分析起来就比较简单了: 这个 ...
- bzoj 2440: [中山市选2011]完全平方数
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #defin ...