标签:ble arc 特定 字典 指定 nsstring 1.3 异步发送 模式
// 不带消息内容
NSNotification *notification1 = [NSNotification notificationWithName:@"notification1"
object:self];
// 带消息内容
NSNotification *notification2 = [NSNotification notificationWithName:@"notification2"
object:self
userInfo:@{@"name":_name, @"age":_age}];
// 发送创建好的消息
[[NSNotificationCenter defaultCenter] postNotification:notification1];
// 直接发送消息,不带消息内容
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification3"
object:self];
// 直接发送消息,带消息内容
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification4"
object:self
userInfo:@{@"name":_name, @"age":_age}];
[[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;
}
// 移除此观察者的所有通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
// 移除指定名字的通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"notification1" object:nil];
- (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];
});
- (void)dequeueNotificationsMatching:(NSNotification *)notification coalesceMask:(NSUInteger)coalesceMask;
参数说明:
notification:通知
coalesceMask:合并方式
// 移除通知,不是立即发布的通知可以被移除
[[NSNotificationQueue defaultQueue] dequeueNotificationsMatching:asyncNotification coalesceMask:0];
UIDeviceOrientationDidChangeNotification // 设备旋转
UIDeviceBatteryStateDidChangeNotification // 电池状态改变
UIDeviceBatteryLevelDidChangeNotification // 电池电量改变
UIDeviceProximityStateDidChangeNotification // 近距离传感器(比如设备贴近了使用者的脸部)
UIKeyboardWillShowNotification // 键盘即将显示
UIKeyboardDidShowNotification // 键盘显示完毕
UIKeyboardWillHideNotification // 键盘即将隐藏
UIKeyboardDidHideNotification // 键盘隐藏完毕
UIKeyboardWillChangeFrameNotification // 键盘的位置尺寸即将发生改变
UIKeyboardDidChangeFrameNotification // 键盘的位置尺寸改变完毕
UIKeyboardFrameBeginUserInfoKey // 键盘刚开始的 frame
UIKeyboardFrameEndUserInfoKey // 键盘最终的 frame(动画执行完毕后)
UIKeyboardAnimationDurationUserInfoKey // 键盘动画的时间
UIKeyboardAnimationCurveUserInfoKey // 键盘动画的执行节奏(快慢)
- (void)dealloc {
// [super dealloc]; // 非 ARC 中需要调用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playFinished)
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:AVPlayerItemDidPlayToEndTimeNotification
object:nil];
标签:ble arc 特定 字典 指定 nsstring 1.3 异步发送 模式
原文地址:https://www.cnblogs.com/CH520/p/9960929.html