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. NuGet学习笔记(2)——vs2015搭建本地NuGet服务器

    搭建本地服务器特别简单,新建一个web空项目,按照下图所示搜索安装即可,之后设置hosts 将www.mynuget.com执向本机 运行里面输入c:\windows\system32\drivers ...

  2. 安装部署ELK系统监控Azure China的NSG和WAF Log

    ELK是一个开源的产品,其官网是:https://www.elastic.co/ ELK主要保护三个产品: Elasticsearch:是基于 JSON 的分布式搜索和分析引擎,专为实现水平扩展.高可 ...

  3. c# OrderBy 实现List升序降序

    本文转载自:http://blog.csdn.net/chadcao/article/details/8730132 1)前台代码 <%@ Page Language="C#" ...

  4. Celery-4.1 用户指南: Configuration and defaults (配置和默认值)

    这篇文档描述了可用的配置选项. 如果你使用默认的加载器,你必须创建 celeryconfig.py 模块并且保证它在python路径中. 配置文件示例 以下是配置示例,你可以从这个开始.它包括运行一个 ...

  5. PHP实现常用排序算法(含示意动图)

    目录 1 快速排序 2 冒泡排序 3 插入排序 4 选择排序 5 归并排序 6 堆排序 7 希尔排序 8 基数排序 总结 作为phper,一般接触算法的编程不多. 但基本的排序算法还是应该掌握. 毕竟 ...

  6. hadoop报错java.io.IOException: Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured

    不多说,直接上干货! 问题详情 问题排查 spark@master:~/app/hadoop$ sbin/start-all.sh This script is Deprecated. Instead ...

  7. 【263】Linux 添加环境变量 & 全局 shell 脚本

    Linux电脑添加环境变量 方法一:通过修改 profile 文件添加环境变量 1. 打开终端,输入[vi /etc/profile],如下所示,点击回车 [ocean@ygs-jhyang-w1 L ...

  8. C++面向对象类的实例题目六

    问题描述: 编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数. 程序代码: #include<iostream> ...

  9. SpringMVC_05 利用spring框架来处理异常

    待更新... 2017年5月13日22:46:52 1 用spring框架来处理异常 将异常抛给spring框架,让spring框架来处理 异常:这样就不需要程序员去捕获异常啦 2 方法一:配置简单异 ...

  10. PCL—关键点检测(Harris)低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5064848.html 除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更 ...