一. 先看下官方对NSNotification通知的解释

1. NSNotification 通知

 @interface NSNotification : NSObject <NSCopying, NSCoding>

  接口通知,继承NSObject,实现NSCopying,NSCoding协议

 A container for information broadcast through a notification center to all registered observers.

  通过通知中心向所有注册观察员进行信息广播的容器

 You don’t usually create your own notifications directly, but instead call the NSNotificationCenter methods.

  你不能使用init去创建,而是调用NSNotificationCenter下的方法

2. NSNotificationCenter 通知中心

 A notification dispatch mechanism that enables the broadcast of information to registered observers.

  一种通知调度机制,该机制允许向注册观察员广播信息

 Each running app has a defaultCenter notification center, and you can create new notification centers to organize communications in particular contexts.

  每个运行的应用程序都有一个默认中心通知中心,您可以创建新的通知中心来组织特定上下文中的通信

 A notification center can deliver notifications only within a single program; if you want to post a notification to other processes or receive notifications from other processes, use NSDistributedNotificationCenter instead.

  通知中心只能在单个程序中传递通知;如果要将通知发布到其他进程或接收来自其他进程的通知,则使用NSdultDeNeTimeCenter代替

3. addObserver(_:selector:name:object:) 添加观察者方法

  Adds an entry to the notification center's dispatch table with an observer, a selector, and an optional notification name and sender.

  使用观察者、选择器和可选通知名称和发送器向通知中心的调度表添加条目

Declaration 声明

*observer 观察者

  作为观察者注册的对象

*aSelector 选择器

  当发出通知时,通知中心将向通知观察者发送通知的选择器

*aName 注册观察员的通知的名称

  当NIL时,通知中心不使用通知的名称来决定是否将其传递给观察者

*anObject 观察者希望接收的通知对象

  当NIL时,通知中心不使用通知的发送方来决定是否将其传递给观察者

4. postNotificationName(_:object:userInfo:) 发送通知消息的方法

  Creates a notification with a given name, sender, and information and posts it to the notification center.

  创建一个带有给定名称、发送者和信息的通知,并将其发布到通知中心

 - (void)postNotificationName:(NSNotificationName)aName

                           object:(id)anObject

                          userInfo:(NSDictionary *)aUserInfo;

aName

  通知的名称

* anObject

  发布通知的对象

* aUserInfo

  关于通知的可选信息

5. removeObserver:name:object: 移除观察者的方法

  从通知中心的调度表中移除指定观察者的所有指定条目。

  如果你的应用程序瞄准了iOS 9和后来的Mac OS 10.11和以后,你就不需要在它的 dealloc 方法中注销观察者


- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observer
         name:(NSString *)aName
        object:(id)anObject;

 二. 通知使用

示例背景: 用到两个ViewController,分别是 NotificationVC,TestVCViewController,当前代码写在NotificationVC中

1. 注册通知
  // 初始化通知中心
NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
// 注册通知
[center addObserver:self selector:@selector(methodTest:) name:nil object:nil];
[center addObserver:self selector:@selector(sadNotif:) name:@"sad" object:nil];
[center addObserver:self selector:@selector(smileNotif:) name:@"smile" object:nil];
[center addObserver:self selector:@selector(sad2Notif:) name:@"sad" object:self]; [center addObserver:test selector:@selector(testNotif:) name:nil object:nil];
[center addObserver:test selector:@selector(sadNotif:) name:@"sad" object:nil];
[center addObserver:test selector:@selector(smileNotif:) name:@"smile" object:nil];
/* 用法说明
上面使用了两个监听器self,test,并且就参数三name,参数四object不同情况分别作出示例
[center addObserver:<#(nonnull id)#>
          selector:<#(nonnull SEL)#>
          name:<#(nullable NSNotificationName)#>
          object:<#(nullable id)#>]
* 参数一 :监听器,即谁要接受这个通知
* 参数二 :回调方法,即接受通知时候,回调这个方法,作出响应
并把通知对象当作 参数 传入
* 参数三 : 通知的名称,标签
如果为nil,可以监听所有此类通知(具体还需要结合参数四)
如果有值,只监听这个名称的所有通知
* 参数四 :通知发布者
如果为nil,不关心是谁发布的通知,可根据参数三获取通知
如果有值,会获取这个值发布的通知,具体还需要看参数三是否一致 四个参数的作用:
参数一: 用于决定回调方法的位置,写在哪个类里面
参数二: 具体的回调方法,参数是通知对象
参数三: 是否需要监听指定的通知名称的通知
参数四: 是否需要监听指定的发布通告者的通知 注意: 如果通知名(参数三),发布通知者(参数四)都为nil
那么这个监听器的方法(参数二)可以回调所有此类通知
**/
2.发布通知:
  // 发出通知

    [center postNotificationName:@"sad" object:nil  userInfo:@{@"title":@"伤心"}];
[center postNotificationName:@"sad" object:self userInfo:@{@"title":@"伤心"}];
[center postNotificationName:@"sad" object:test userInfo:@{@"title":@"伤心"}]; [center postNotificationName:@"smile" object:nil userInfo:@{@"title":@"开心"}];
[center postNotificationName:@"smile" object:test userInfo:@{@"title":@"开心"}];
[center postNotificationName:@"smile" object:self userInfo:@{@"title":@"开心"}]; /* 发布通知
[center postNotificationName:<#(nonnull NSNotificationName)#>
                  object:<#(nullable id)#>
                 userInfo:<#(nullable NSDictionary *)#>]
* 第一个参数,通知的方法名字,不能为nil
* 第二个参数,通知的发布者,可以为nil
* 第三个参数,通知所传递的信息,为字典格式
*/
3. 回调方法:
// self 中(NotificationVC)
- (void)smileNotif:(NSNotification *)notif{
NSLog(@"------1----%@/n",notif.userInfo);
}
- (void)sadNotif:(NSNotification *)notif{
NSLog(@"------2----%@/n",notif.userInfo);
}
- (void)methodTest:(NSNotification *)notif{
NSLog(@"------3----%@/n",notif.userInfo);
}
- (void)sad2Notif:(NSNotification *)notif{
NSLog(@"------4----%@/n",notif.userInfo);
} // TestVCViewController中
- (void)testNotif:(NSNotification *)notif{
NSLog(@"-----5-----%@/n",notif.userInfo);
}
- (void)smileNotif:(NSNotification *)notif{
NSLog(@"-----6-----%@/n",notif.userInfo);
}
- (void)sadNotif:(NSNotification *)notif{
NSLog(@"-----7-----%@/n",notif.userInfo);
} // 由于打印的太多,这里不提供打印结果了,有兴趣的可以copy,断点打印,查看 参数不同 的 注册通知 之间的区别

  4. 移除通知:

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:test];
[[NSNotificationCenter defaultCenter] removeObserver:self];
} // 移除方法
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject; /*
removeObserver:是删除通知中心保存的调度表指定某个观察者的所有入口 removeObserver:name:object:是删除通知中心保存的调度表中某个观察者的指定某个入口
注意参数notificationObserver为要删除的观察者,不能为nil
**/ // 通常这样写 在viewWillAppear 中 addObserver,在viewWillDisappear 中 removeObserver
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(xxxx) name:@"xxxx" object:nil];
} - (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"xxxx" object:nil];
}
三. 扩展
1.block 通知
 // 官方语法:
- (id <NSObject>)addObserverForName:(nullable NSNotificationName)name
                     object:(nullable id)obj
                     queue:(nullable NSOperationQueue *)queue
                  usingBlock:(void (^)(NSNotification *note))block API_AVAILABLE(macos(10.6),
ios(4.0), watchos(2.0), tvos(9.0)); /**
// The return value is retained by the system, and should be held onto by the caller in
// order to remove the observer with removeObserver: later, to stop observation.
返回值由系统保留,并由调用方保存。
顺序删除观测者,稍后,停止观察。
* 参数一 : 通知名称
* 参数二 : 监听者,即谁监听发出的通知
* 参数三 : 队列,决定block在哪个线程中区执行
如果传入nil,在发布通知的线程中去执行,即发布通知在哪个线程,block就在哪个线程中区执行
如果是耗时操作,放在子线程
* 参数四 : 监听到通知后的block回调 注意 : block函数返回值(示例:上文声明的 id object),由当前调用方保存,最后要移除
*/ object = [[NSNotificationCenter defaultCenter]addObserverForName:@"good"
                                     object:self
                                      queue:nil
                                   usingBlock:^(NSNotification * _Nonnull note)
  {
// 监听到通知会调用,不用单独写方法
NSLog(@"-----8-----%@/n",note.userInfo);
}]; // object 为声明的 id object [center postNotificationName:@"good" object:self userInfo:@{@"good":@"好极了"}];
[center postNotificationName:@"sad" object:self userInfo:@{@"sad":@"坏死了"}];

2.异步调用通知

即 通知放在异步方法中

* NSNotificationCenter消息的接受线程是基于发送消息的线程的,即 同步的

*  有时,你发送的消息可能不在主线程,但是操作UI必须在主线程,不然会出现不响应的情况。所以,在收到消息通知的时候,注意选择你要执行的线程

*  一般用于通知中执行耗时操作,异步执行,保证UI主线程的流程,不会“假死,卡死”

总结

通知通常用于监听需求属性,当属性发生变化,发出通知,通过回调方法作出改变,响应通知

参考博客:

iOS中 本地通知/本地通知详解 韩俊强的博客 (ps: 本地通知讲解的很详细,有兴趣可以下载他的demo)

iOS中通知中心(NSNotificationCenter)的使用总结

IOS中通知中心(NSNotificationCenter)的使用总结

IOS 通知详解+异步发送和接收通知

  

IOS NSNotification 通知的更多相关文章

  1. iOS NSNotification通知

    通知中心(NSNotificationCenter) 通知(NSNotification) 一个完整的通知一般包含3个属性:(注意顺序) - (NSString *)name;  通知的名称 - (i ...

  2. iOS - Notification 通知

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

  3. IOS Notification 通知中心

    1.     通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...

  4. How Not to Crash #3: NSNotification通知引起的崩溃

    How Not to Crash #3: NSNotification通知引起的崩溃html, body {overflow-x: initial !important;}html { font-si ...

  5. IOS中通知中心(NSNotificationCenter)

    摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广.   IOS中通知中心NSNotificationCenter应用总结 一.了 ...

  6. ios 消息通知

    苹果的通知分为本地通知和远程通知,这里主要说的是远程通知 历史介绍 iOS 3 - 引入推送通知UIApplication 的 registerForRemoteNotificationTypes 与 ...

  7. iOS中通知传值

    NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面.   思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...

  8. IOS开发-通知与消息机制

    在多数移动应用中不论什么时候都仅仅能有一个应用程序处于活跃状态.假设其它应用此刻发生了一些用户感兴趣的那么通过通知机制就能够告诉用户此时发生的事情. iOS中通知机制又叫消息机制,其包含两类:一类是本 ...

  9. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

随机推荐

  1. java中json解析,xml解析

    抓取网页内容,会返回json或者xml(html)格式的数据. 为了方便的对上述两种格式的数据进行解析,可采用解析工具. JsonPath https://github.com/jayway/Json ...

  2. .net编程知识点

    一.编程思想 OOP(面向对象) 面向对象三大特性(多态如何体现)及五项原则 AOP(面向切面编程) 面向切面编程静态植入和动态植入 二.c#23种设计模式 三.Castle是针对.NET平台的一个开 ...

  3. 第一个servet(用注解),不用web.xml

    环境: idea 1.新建模块 2.在蓝色src下新建一个包com.test 3.在包下新建servlet 4.写代码 package com.test; import javax.servlet.S ...

  4. webpack初步搭建Vue项目

    对文件进行打包 1. cnpm i -D webpack webpack-cli 本地热更新 1. cnpm i -D webpack-dev-server 处理图片资源 url-loader依赖fi ...

  5. bootstrap模态框实现相对定位拖拽

    1.正常的拖拽是用绝对定位absolute来实现的,可是bootstrap的模态框是用relative,为了统一更改方便,就照着相对定位来实现拖拽效果. $(".modal .modal-h ...

  6. vue1.0 与 Vue2.0的一些区别 及用法

    1.Vue2.0的模板标记外必须使用元素包起来: eg:Vue1.0的写法 <!DOCTYPE html> <html> <head> <meta chars ...

  7. Vue基础知识之vue-resource和axios

    Vue基础知识之vue-resource和axios  原文链接:http://www.cnblogs.com/Juphy/p/7073027.html vue-resource Vue.js是数据驱 ...

  8. Angular JS例子 ng-repeat遍历输出

    首先要有Angular的插件:然后才开始遍历 :<!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. cordova 开发 android app 简要流程

    1. 安装cordova:npm install -g cordova --registry=https://registry.npm.taobao.org 2. 创建cordova工程:进入工作目录 ...

  10. 解决Tensorflow源码安装的之后TensorBoard 无法使用的问题

    作者  cnblog 修雨轩陈 我是按照 Tensorflow 下 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3 ...