NSNotificationCenter(通知中心)(例UIDevice通知)
来源:互联网 发布:骨科医生 知乎 编辑:程序博客网 时间:2024/06/06 04:02
•通知的发布
•通知的监听
•通知的移除
1,通知(NSNotification)
•一个完整的通知一般包含3个属性:
Ø - (NSString*)name;//通知的名称
Ø - (id)object;//通知发布者(是谁要发布通知)
Ø - (NSDictionary*)userInfo;//一些额外的具体信息
•初始化一个通知(NSNotification)对象
Ø + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject;
Ø + (instancetype)notificationWithName:(NSString*)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;
Ø - (instancetype)initWithName:(NSString*)name object:(id)object userInfo:(NSDictionary*)userInfo;
2,发布通知方法
通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知
Ø- (void)postNotification:(NSNotification*)notification;
发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
Ø- (void)postNotificationName:(NSString *)aNameobject:(id)anObject;
发布一个名称为aName的通知,anObject为这个通知的发布者
Ø- (void)postNotificationName:(NSString *)aNameobject:(id)anObjectuserInfo:(NSDictionary *)aUserInfo;
发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为一些额外信息
3,注册通知监听器
通知中心(NSNotificationCenter)提供了相应的方法注册监听器(Observer)来监听通知的发布
• - (void)addObserver:(id)observerselector:(SEL)aSelectorname:(NSString *)aNameobject:(id)anObject;
参数解析:
Ø observer:监听器,即谁要接收这个通知
Ø aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
Ø aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
Ø anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
•- (• - (id)addObserverForName:(NSString *)nameobject:(id)objqueue:(NSOperationQueue *)queueusingBlock:(void (^)(NSNotification *note))block;
参数解析:
Øname:通知的名称
Øobj:通知发布者
Øblock:收到对应的通知时,会回调这个block
Øqueue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
4,取消注册通知监听器
•通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃
•通知中心提供了相应的方法来取消注册监听器
Ø- (void)removeObserver:(id)observer;
Ø-(void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
•一般在监听器销毁之前取消注册(如在监听器中加入下列代码):
- (void)dealloc {
[super dealloc];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
5,UIDevice通知
•UIDevice类提供了一个单粒对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)
•通过[UIDevicecurrentDevice]可以获取这个单粒对象
•UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:
ØUIDeviceOrientationDidChangeNotification// 设备旋转
ØUIDeviceBatteryStateDidChangeNotification// 电池状态
ØUIDeviceBatteryLevelDidChangeNotification// 电池电量
ØUIDeviceProximityStateDidChangeNotification// 近距离传感器(比如设备贴近了使用者的脸部)
0 0
- NSNotificationCenter(通知中心)(例UIDevice通知)
- NSNotificationCenter(通知中心)
- 通知中心(NSNotificationCenter)
- 通知中心NSNotificationCenter
- 通知中心 NSNotificationCenter
- IOS NSNotificationCenter 通知中心
- 通知中心(NSNotificationCenter)
- NSNotificationCenter 通知中心
- 通知中心NSNotificationCenter
- NSNotificationCenter通知中心
- IOS NSNotificationCenter 通知中心
- NSNotificationCenter通知中心
- iOS通知中心(NSNotificationCenter)
- 通知中心NSnotificationCenter
- iOS通知中心NSNotificationCenter
- 通知中心(NSNotificationCenter)
- NSNotificationCenter通知中心
- NSNotificationCenter通知中心
- 6.代理模式
- 内向不是缺点:关于性格内向者的10个误解
- android笔记6-常用控件的介绍二
- 7.单例模式
- 8.迭代器模式
- NSNotificationCenter(通知中心)(例UIDevice通知)
- 数据库语句
- 9.访问者模式
- hdu2176取(m堆)石子游戏 nim hdu1848Fibonacci again and again sg
- Mysql 重装问题的解决方法
- 10.观察者模式
- 11.建造者模式
- poj2409 Let it Bead polya定理
- Oracle相同用户相同库Copy表结构