1、自定义发送 Notification 的使用

  • 1.1 通知(消息)的创建 ---------------详细介绍篇

    // 不带消息内容
    NSNotification *notification1 = [NSNotification notificationWithName:@"notification1"
    object:self];
    // 带消息内容
    NSNotification *notification2 = [NSNotification notificationWithName:@"notification2"
    object:self
    userInfo:@{@"name":_name, @"age":_age}];
  • 1.2 发送通知

    // 发送创建好的消息
    [[NSNotificationCenter defaultCenter] postNotification:notification1]; // 直接发送消息,不带消息内容
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification3"
    object:self];
    // 直接发送消息,带消息内容
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification4"
    object:self
    userInfo:@{@"name":_name, @"age":_age}];
  • 1.3 注册通知(观察者)

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(notification1Sel)
    name:@"notification1"
    object:nil];
    // 通知触发方法,通知无内容
    - (void)notification1Sel {
    } ---------------------------------------------------------------------------- [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(notification2Sel:)
    name:@"notification2"
    object:nil]; // 通知触发方法,通知有内容
    - (void)notification2Sel:(NSNotification *)notification {
    // 接收用户消息内容
    NSDictionary *userInfo = notification.userInfo;
    }
  • 1.4 移除通知(观察者)

    // 移除此观察者的所有通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // 移除指定名字的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification1" object:nil];

2、异步发送 Notification 的使用

  • 2.1 发送异步通知

    - (void)enqueueNotification:(NSNotification *)notification
postingStyle:(NSPostingStyle)postingStyle; - (void)enqueueNotification:(NSNotification *)notification
postingStyle:(NSPostingStyle)postingStyle
coalesceMask:(NSNotificationCoalescing)coalesceMask
forModes:(nullable NSArray<NSString *> *)modes;
参数说明:
notification:通知
postingStyle:发布方式
coalesceMask:合并方式
modes :运行循环模式,nil 表示 NSDefaultRunLoopMode
NSPostingStyle :发布方式
NSPostWhenIdle = 1, :空闲时发布
NSPostASAP = 2, :尽快发布
NSPostNow = 3 :立即发布
NSNotificationCoalescing :合并方式
NSNotificationNoCoalescing = 0, :不合并
NSNotificationCoalescingOnName = 1, :按名称合并
NSNotificationCoalescingOnSender = 2 :按发布者合并 // 创建通知
NSNotification *asyncNotification = [NSNotification notificationWithName:@"asyncNotification" object:self];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 将通知添加到发送队列中,发送通知
[[NSNotificationQueue defaultQueue] enqueueNotification:asyncNotification postingStyle:NSPostWhenIdle];
});
```
- ## 2.2 移除异步通知
``` Objective-C
- (void)dequeueNotificationsMatching:(NSNotification *)notification coalesceMask:(NSUInteger)coalesceMask;
参数说明:
notification:通知
coalesceMask:合并方式 // 移除通知,不是立即发布的通知可以被移除
[[NSNotificationQueue defaultQueue] dequeueNotificationsMatching:asyncNotification coalesceMask:0];
``` # 3、系统通知的使用
- ## 3.1 UIDevice 通知
- UIDevice 对象会不间断地发布一些通知,下列是 UIDevice 对象所发布通知的名称常量:
``` Objective-C
UIDeviceOrientationDidChangeNotification // 设备旋转
UIDeviceBatteryStateDidChangeNotification // 电池状态改变
UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)
``` - ## 3.2 键盘通知
- 键盘状态改变的时候,系统会发出一些特定的通知:
``` Objective-C
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
```
- 系统发出键盘通知时,会附带一下跟键盘有关的额外信息(字典),字典常见的 key 如下:
``` Objective-C
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的 frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的 frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
``` - ## 3.3系统发送 Notification 的使用
- 一般在监听器销毁之前取消注册(如在监听器中加入下列代码):
``` Objective-C
- (void)dealloc {
// [super dealloc]; // 非 ARC 中需要调用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
```
- 在注册、移除通知时,通知名称标示(aName)使用系统定义的标示。
- **注册通知(观察者)**
``` Objective-C
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playFinished)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
```
- **移除通知(观察者)**
``` Objective-C
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
```
05-27 08:38