转自: http://blog.sina.com.cn/s/blog_65de27980101130r.html
http://www.cnblogs.com/superhappy/archive/2012/01/31/2333177.html
http://www.cnblogs.com/pengyingh/articles/2367374.html
http://www.189works.com/article-37622-1.html
http://blog.sina.com.cn/s/blog_793cad6e0100sa6x.html
http://blog.sina.com.cn/s/blog_65de27980101130r.html
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate实现的,例如 View 加载完后会触发 viewDidLoad。 Apple 还为我们提供了另一种通知响应方式,那就是NSNotification,系统中(UIKeyboardDidShowNotific ation 等) 以及某些第三方组件(例如ASIHTTPRequest 的 kReachabilityChangedNoti fication 等)。
NSNotificationCenter 较之于 Delegate可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter的通信原理使用了观察者模式:
1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的Selector 或 Block
2. NSNotificationCenter 在某个时机激发事件(以字符串命名)
3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或 Block
使用 NSNotificationCenter 的步骤示例代码:
1. 定义一个事件到来时该执行的方法:
1
2
3
4
5
6
7
|
-(
void
)execute:(
NSNotification
*)notification {
//do something when receivednotification
//notification.name is@"NOTIFICATION_NAME"
if
(notification.object && [notification.objectisKindOfClass:[Test
class
]]){
//do something
}
}
|
2. 注册观察者:
1
2
3
4
|
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(execute:)
name:@
"NOTIFICATION_NAME"
object:
nil
];
|
使用默认的通知中心,上面代码的意义的,观察者 self 在收到名为 @"NOTIFICATION_NAME" 的事件是执行@selector(execute:),最后一个参数是表示会对哪个发送者对象发出的事件作出响应,nil时表示接受所有发送者的事件。
还有一种注册观察者的方式是用方法:
- (id)addObserverForName:(NSString*)name object:(id)obj queue:(NSOperationQueue*)queue usingBlock:(void (^)(NSNotification*))block
3. 激发事件,即通知相应的观察者
1
2
3
4
5
6
7
|
[[
NSNotificationCenter
defaultCenter]postNotificationName:@
"NOTIFICATION_NAME"
object:
nil
];
//或者用下面几行代码,明确的 notification 示例
Test*test = [[Test alloc] init];
NSNotification
*notification = [
NSNotification
notificationWithName:@
"NOTIFICATION_NAME"
object:test];
[[
NSNotificationCenter
defaultCenter]postNotification:notification];
|
这里的 object 参数对应到方法 -(void)execute:(NSNotification *)notification 里的notification.object, name 就是 notification.name。
代码到这里,方法 -(void)execute:(NSNotification *)notification 就会得到执行了。
说明:我们上面用的 [NSNotificationCenterdefaultCenter] 同一个实例,你也可以使用自己的NSNotificationCenter,如:
NSNotificationCenter*notificationCenter = [[NSNotificationCenteralloc]init];
事件只会在当前的 NotificationCenter 中广播,不同的 NotificationCenter之间的事件通知互不相干。
NSNotificationCenter 比之 Delegate的好处就是事件发出者与响应者可以完全不认识,例如你在某个类中注册了 A 观察者某 E 事件的响应,你可以在程序的任何代码 X 中激发E,A 的相应选择器即被触发,对象 A 与 X 完全是松散的。
最后,你的观察者如果对一些事件没兴趣了,应该从 NotificationCenter 中移除掉:
1
2
3
|
[[
NSNotificationCenter
defaultCenter] removeObserver:
self
name:@
"NOTIFICATION_NAME"
object:test];
//object 与注册时相同
//或[[NSNotificationCenter defaultCenter]removeObserver:self];
|
系统里定义了许多的 XxxNotification 名称,其实只要 Cmd+Shift+O 打开 Open Quickly,输入nsnotification 或者 uinotification 可以看到许多以 Notification结尾的变量定义,由变量名称也能理解在什么时候会激发什么事件,一般都是向 [NSNotificationCenterdefaultCenter] 通知的。
比如你想对系统的某些事件时作出响应只要注册一个观察者即可,想要在每次键盘显示后得到通知就得关心UIKeyboardDidShowNotific
1
2
3
4
|
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(keyboardDidShow:)
name:
UIKeyboardDidShowNotific
object:
nil
];
|
键盘显示后就会执行 self 的 keyboardDidShow 方法。
我们可以多看看第三方 Objective-C 库是怎么运用的 NSNotificationCenter。
Cocoa 给我们另一种事件通知模型就是 KVO(Key-Value Obsering),基于NSKeyValueObserving 非正式协议。
参考:1. NSNotification Class Reference
2. NSNotificationCenter 的使用
3. iPhone之NSNotificationCenter使用方法
4. Key-Value Observing Programming Guide
5. AppDelegate VS. NSNotificationCenter
http://www.cnblogs.com/superhappy/archive/2012/01/31/2333177.html
新建一个继承于UIViewControll的类,并在.m中添加如下代码
-(void)doSomeThing:(NSNotification *)aNote { NSDictionary *dict = [aNote object]; NSLog(@"%@",dict); } - (void)loadView { [super loadView]; NSString *myString = @"some Value"; NSDictionary *myDict = [[NSDictionary alloc]initWithObjectsAndKeys:myString,@"first", nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomeThing:) name:@"notification" object:nil]; [[NSNotificationCenter defaultCenter]postNotificationName:@"notification" object:myDict]; }
设置通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomeThing:) name:@"notification" object:nil];
addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object: 对象,当设定为nil时,默认为所有名称为以上消息名称的(notification)都接收不管发送者是谁。
发送通知:
[[NSNotificationCenter defaultCenter]postNotificationName:@"notification" object:myDict];
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:对象,当设定为nil时,默认为所有名称为以上消息名称的(notification)都发送。
小例子仅仅是在一个类下的通知,实际来说通知更多的应用于不同的类之间传递信息。
http://www.cnblogs.com/pengyingh/articles/2367374.html
作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的.
注册通知:即要在什么地方接受消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];
参数介绍:
addObserver: 观察者,即在什么地方接收通知;
selector: 收到通知后调用何种方法;
name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
发送通知:调用观察者处的方法。
[[NSNotificationCenter defaultCenter] postNotificationName:@"mytest" object:searchFriendArray];
参数:
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
注册方法的写法:
- (void) mytest:(NSNotification*) notification
{
id obj = [notification object];//获取到传递的对象
}
附:注册键盘升启关闭消息
|
在有需要的类中,发送消息
//发送消息出去,这里的对象是一个数组:saveImageArray
[[NSNotificationCenter defaultCenter] postNotificationName:@"postData" object:saveImageArray];
所有亲朋好友给我给包(发送消息),,,
//注册一个observer来响应消息的传送
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(PostImage:)//接收消息方法
name:@"postData"//消息识别名称
object:nil];
举个例子,过年了,准备一个大的钱包(注册一个OBserver),嘿嘿,,,,
//实现方法
-(void)PostImage:(NSArray *)array
{
//接收传送过来的消息
}
我把红包都收起来,(接收消息)
//移除observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"postData" object:nil];
红包都收完了,哈哈,亲朋好友回家咯。。
补充:http://www.189works.com/article-37622-1.html
NSNotification
个人觉得用这个东西在不同的viewcontroller间传东西很方便的
发消息
[[NSNotificationCenter defaultCenter] postNotificationName:@"popView"/*消息名字,在添加监听时会用到*/
object:@"ShowHomeLineViewController"/*传的参数,多个参数就可以用数组啦*/];
收消息
1、添加监听:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(Show:)/*收到消息后的响应函数*/ name:@"popView"/*消息名字,在发消息时 指定的*/ object:nil];
2、消息处理(实现前面的Show:函数)
-(void)Show:(NSNotification*)notification
{
NSString* str = (NSString*)[notification object];//这里取出刚刚从过来的字符串
}
3、不要忘记移除监听 一般我们在dealloc方法中做这件事
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"popView" object:nil];
http://blog.sina.com.cn/s/blog_793cad6e0100sa6x.html
什么是Notification?
这个要求其实也很容易实现. 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏. 对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做 observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). center将该notifications转发给所有注册对该notification感兴趣的对象. 我们把这些发送notification的对象叫做poster
很多的标准Cocoa类会发送notifications: 在改变size的时候,Window会发送notification; 选择table view中的一行时,table view会发送notification;我们可以在在线帮助文档中查看到标准cocoa对象发送的notification
在对象释放前,我们必须从notificationcenter移除我们注册的observer. 一般我们在dealloc方法中做