美文网首页
NSNotification

NSNotification

作者: CoderRH | 来源:发表于2016-08-09 11:34 被阅读6次
  • 一个完整的通知一般包含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;
  • 通知中心(NSNotificationCenter)提供了相应的方法来帮助发布通知
//发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
- (void)postNotification:(NSNotification *)notification;

//发布一个名称为aName的通知,anObject为这个通知的发布者
- (void)postNotificationName:(NSString *)aName object:(id)anObject;

//发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
- (void)postNotificationName:(NSString *)aName 
                      object:(id)anObject
                    userInfo:(NSDictionary *)aUserInfo;
  • 通知中心(NSNotificationCenter)提供了方法来注册一个监听通知的监听器(Observer)
observer:监听器,即谁要接收这个通知
aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知

- (void)addObserver:(id)observer
           selector:(SEL)aSelector 
               name:(NSString *)aName 
             object:(id)anObject;
name:通知的名称
obj:通知发布者
block:收到对应的通知时,会回调这个block
queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行

- (id)addObserverForName:(NSString *)name 
                  object:(id)obj 
                   queue:(NSOperationQueue *)queue          
              usingBlock:(void (^)(NSNotification *note))block;
  • 通知中心不会保留(retain)监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃

  • 通知中心提供了相应的方法来取消注册监听器

   - (void)removeObserver:(id)observer;

   - (void)removeObserver:(id)observer
                  name:(NSString *)aName 
                object:(id)anObject;
  • 一般在监听器销毁之前取消注册(如在监听器中加入下列代码):
 - (void)dealloc {
       //[super dealloc];  非ARC中需要调用此句
       [[NSNotificationCenter defaultCenter] removeObserver:self];
}   

UIDevice通知

UIDevice类提供了一个单粒对象,它代表着设备,通过它可以获得一些设备相关的信息,比如电池电量值(batteryLevel)、电池状态(batteryState)、设备的类型(model,比如iPod、iPhone等)、设备的系统(systemVersion)

通过[UIDevice currentDevice]可以获取这个单粒对象

UIDevice对象会不间断地发布一些通知,下列是UIDevice对象所发布通知的名称常量:

UIDeviceOrientationDidChangeNotification // 设备旋转
UIDeviceBatteryStateDidChangeNotification // 电池状态改变
UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)

相关文章

  • NSNotification

    发通知 NSNotification *deleteMyCommemtN =[NSNotification not...

  • 通知使用

    创建对象 NSNotification *notification =[NSNotification notifi...

  • iOS通知中心

    有关的类 NSNotification NSNotificationCenter NSNotification N...

  • 通知中心(NSNotificationCenter)总结

    一、简介 1. NSNotification 理解 NSNotification@property (readon...

  • 推送通知-本地推送

    iOS推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的...

  • NSNotification是同步还是异步?和delegate相

    1、NSNotification是同步还是异步?默认情况下,创建的NSNotification是同步的,发布通知 ...

  • iOS 推送后台语音播报

    推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的,不可...

  • iOS本地推送

    1. 推送通知简介 1.1: 这里说的推送通知跟NSNotification有所区别 NSNotification...

  • NSNotification

    当你定义你自己的 NSNotification的时候你应该把你的通知的名字定义为一个字符串常量,就像你暴露给其他类...

  • NSNotification

    通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 vie...

网友评论

      本文标题:NSNotification

      本文链接:https://www.haomeiwen.com/subject/kofdsttx.html