NSNotificationCenter 的使用详解
来源:互联网 发布:excel不规则数据分列 编辑:程序博客网 时间:2024/05/29 15:04
转载:http://unmi.cc/nsnotificationcenter-usage/
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad。 Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification,系统中(UIKeyboardDidShowNotification 等) 以及某些第三方组件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。
NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式:
1. NSNotificationCenter 注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的 Selector 或 Block
2. NSNotificationCenter 在某个时机激发事件(以字符串命名)
3. 观察者在收到感兴趣的事件时,执行相应的 Selector 或 Block
使用 NSNotificationCenter 的步骤示例代码:
1. 定义一个事件到来时该执行的方法:
- (
void
)execute:(
NSNotification
*)notification {
//do something when received notification
//notification.name is @"NOTIFICATION_NAME"
if
(notification.object && [notification.object isKindOfClass:[Test
class
]]){
//do something
}
}
2. 注册观察者:
[[
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. 激发事件,即通知相应的观察者
[[
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 就会得到执行了。
说明:我们上面用的 [NSNotificationCenter defaultCenter] 同一个实例,你也可以使用自己的 NSNotificationCenter,如:
NSNotificationCenter *notificationCenter = [[NSNotificationCenter alloc]init];
事件只会在当前的 NotificationCenter 中广播,不同的 NotificationCenter 之间的事件通知互不相干。
NSNotificationCenter 比之 Delegate 的好处就是事件发出者与响应者可以完全不认识,例如你在某个类中注册了 A 观察者某 E 事件的响应,你可以在程序的任何代码 X 中激发 E,A 的相应选择器即被触发,对象 A 与 X 完全是松散的。
最后,你的观察者如果对一些事件没兴趣了,应该从 NotificationCenter 中移除掉:
[[
NSNotificationCenter
defaultCenter] removeObserver:
self
name:@
"NOTIFICATION_NAME"
object:test];
//object 与注册时相同
//或[[NSNotificationCenter defaultCenter] removeObserver:self];
系统里定义了许多的 XxxNotification 名称,其实只要 Cmd+Shift+O 打开 Open Quickly,输入 nsnotification 或者 uinotification 可以看到许多以 Notification 结尾的变量定义,由变量名称也能理解在什么时候会激发什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。
比如你想对系统的某些事件时作出响应只要注册一个观察者即可,想要在每次键盘显示后得到通知就得关心 UIKeyboardDidShowNotification 事件:
[[
NSNotificationCenter
defaultCenter] addObserver:
self
selector:
@selector
(keyboardDidShow:)
name:
UIKeyboardDidShowNotification
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://unmi.cc/nsnotificationcenter-usage/, 来自 隔叶黄莺 Unmi Blog
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- NSNotificationCenter 的使用详解
- 一个静态UITabelView的解决方案----CTStaticTableView
- 微信JS接口 分享到朋友圈 分享给朋友 分享到QQ 拍照或从手机相册中选图 识别音频并返回识别结果 使用微信内置地图查看位置
- Python模块学习 ---- datetime
- UI基础—UITableView
- java 利用反射机制,获取实体所有属性和方法,并对属性赋值
- NSNotificationCenter 的使用详解
- cocos node sceen
- 大数据
- 1037. Magic Coupon (25)
- 你们猜对了:12306确实是让淘宝做的
- GRE issue写作范文:竞争利弊问题
- 全球移动应用用户参与度分析
- PHP 数组操作方法
- DB2命令窗口和命令行处理器