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];
```

Notification通知代码简洁使用的更多相关文章

  1. iOS - Notification 通知

    1.Notification 通知中心实际上是在程序内部提供了消息广播的一种机制,它允许我们在低程度耦合的情况下,满足控制器与一个任意的对象进行通信的目的.每一个 iOS 程序(即每一个进程)都有一个 ...

  2. [iOS基础控件 - 6.10] Notification 通知机制

    A.定义      iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知      任何对象都能够在NSNotificationCenter发布通知,发 ...

  3. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  4. emwin之CHECKBOX控件的通知代码的响应规则

    @2018-08-28 [小记] 在 case WM_INIT_DIALOG: 中使用 CHECKBOX_SetState()函数改变了复选框状态,就会产生 WM_NOTIFICATION_VALUE ...

  5. 配置 SQL Server 2008 Email 发送以及 Job 的 Notification通知功能

    SQL Server 2008配置邮件的过程就不写了,网上的案例太多了. http://www.cnblogs.com/woodytu/p/5154526.html 这个案例就不错. 主要写下配置完后 ...

  6. 适配 通知 Notification 通知渠道 前台服务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  7. Notification (通知)的 新版和旧版用法

    Notification (通知)的 新版和旧版用法   一.先来看旧版,Api 11 之前的用法: NotificationManager manager = (NotificationManage ...

  8. 配置 SQL Server Email 发送以及 Job 的 Notification通知功能

    配置 SQL Server Email 发送以及 Job 的 Notification通知功能 在与数据库相关的项目中, 比如像数据库维护, 性能警报, 程序出错警报或通知都会使用到在 SQL Ser ...

  9. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

随机推荐

  1. java List 等份截取

    /** * 描述: 等份截取 * @author eyesmooon * @param list * @param size * @return * @date:2018年11月8日 下午8:00:3 ...

  2. Machine Learning的Python环境设置

    Machine Learning目前经常使用的语言有Python.R和MATLAB.如果采用Python,需要安装大量的数学相关和Machine Learning的包.一般安装Anaconda,可以把 ...

  3. RCE、exp、Exploit、Exploit Pack、exp-gui、Payload、MetaSploit都是啥

    对于走在安全路上的小菜来说,这几个exp.Exploit.Exploit Pack.exp-gui.Payload.MetaSploit名词着实把人转的不轻,下面给大家解释下: RCE,remote ...

  4. 开发环境入门 linux基础 (部分) 归档 压缩 Vi编译器 系统分区

    归档 压缩 Vi编译器 系统分区 1.使用cat命令进行文件的纵向合并          1) 使用cat命令实现文件的纵向合并:          a) 例如:将用户信息数据库文件和组信息数据库文件 ...

  5. mysql 回顾小练习

    Student(id,sname,age,sex) 学生表 Course(id,cname,t_id) 课程表 SC(s_id,c_id,score) 成绩表 Teacher(id,Tname) 教师 ...

  6. 2015.5.11 string与byte[]相互转换

    string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); 反过来,byte[]转成stri ...

  7. 清空select标签中option选项的4种不同方式

    转自:https://blog.csdn.net/pt_sm/article/details/53521560 方法一 document.getElementById("selectid&q ...

  8. xcode中用pods管理第三方库<转>

    安装pods :http://www.cnblogs.com/wangluochong/p/5567082.html 史上最详细的CocoaPods安装教程 --------------------- ...

  9. Maven构建war项目添加版本号

    上午接到一个新的需求,项目的war包打包之后,放在了阿里的OSS上,供其他项目下载更新时使用,但是只有一个项目名,也就是pom的artifactId,预期的结果是要加上一个版本号,能区分出是什么时候打 ...

  10. java线程的三种实现方式

    线程实现的三种种方式: 一个是继承Thread类,实现run()方法: 一个是实现Runnable接口,实现run()方法: 一个是实现Callable接口,实现call()方法:该方式和实现Runn ...