通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制。把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象。这句话其实已经很明显的告诉我们要如何使用通知了。第一步:在需要的地方注册要观察的通知,第二步:在某地方发送通知。(这里注意:发送的通知可能是我们自定义的,也可能是系统的)。

一,使用通知

第1中创建通知方法

//注意,通知的使用是有先后顺序的

//一定要先监听通知,然后在发送通知

 //第一种方法
// 添加一个通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil]; //发送一个通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
  //发送通知也可以传递一些参数
[NSNotificationCenter defaultCenter] postNotificationName:<#(nonnull NSNotificationName)#> object:<#(nullable id)#> userInfo:<#(nullable NSDictionary *)#>

可以在监听的通知的方法获取通知的信息

- (void)addNotification {

    NSLog(@"接受通知");
}

最后要移除通知

- (void)dealloc {

    //移除观察者 self
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

第2种通知的使用使用 block 比较方便简单

这个方法需要一个id类型的值接受

@property (nonatomic, weak) id observe;
    //第二种方法
//Name: 通知的名称
//object:谁发出的通知
//queue: 队列,决定 block 在哪个线程中执行, nil 在发布通知的线程中执行
//usingBlock: 只要监听到通知,就会执行这个 block
//这个通知返回一个 id 这个通知同样需要移除
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"收到了通知");
}]; [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];

同样,这里也需要移除通知,但是这里的观察者不是 self 而是 _observe

- (void)dealloc {

    //移除观察者 _observe
[[NSNotificationCenter defaultCenter] removeObserver:_observe];
}

二 .通知在多线程中的使用

通知在多线程中使用

//通知在接收的方法跟发送通知所在的线程中一样

异步发送通知, 主线程监听通知, 接收通知的方法在子线程中

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //发送通知
dispatch_async(dispatch_get_global_queue(, ), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
});
} - (void)viewDidLoad {
[super viewDidLoad]; //多线程中使用通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil]; } - (void)addNotification {
NSLog(@"接受通知");
NSLog(@"%@",[NSThread currentThread]); //主线程:监听通知,异步发送通知
//总结: 接受通知的代码由发出通知的线程
//更新 UI
dispatch_sync(dispatch_get_main_queue(), ^{
//更新 UI
});
}

控制台输出 : 异步

-- ::05.537 通知多线程使用[:] 接受通知
-- ::05.538 通知多线程使用[:] <NSThread: 0x60800026d8c0>{number = , name = (null)}

主线程中发送通知,异步线程中监听通知

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //主线程中发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil];
} - (void)viewDidLoad {
[super viewDidLoad]; //多线程中使用通知 //异步监听通知 dispatch_async(dispatch_get_global_queue(, ), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addNotification) name:@"tongzhi" object:nil];
});
} - (void)addNotification {
NSLog(@"接受通知");
NSLog(@"%@",[NSThread currentThread]); //异步:监听通知,主线程发送通知
//总结: 接受通知的代码由发出通知的线程 }

控制台输出

-- ::40.160 通知多线程使用[:] 接受通知
-- ::40.160 通知多线程使用[:] <NSThread: 0x600000072ac0>{number = , name = main}

使用 block 创建通知的方法,

queue :[NSOperationQueue mainQueue] 这样都会在主线程中执行block 中代码
    _observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"收到了通知");
}];

控制台输出 : 主线程中执行

-- ::39.277 通知多线程使用[:] 收到了通知
-- ::39.277 通知多线程使用[:] <NSThread: 0x60000006ea40>{number = , name = main}

iOS中通知中心NSNotificationCenter应用总结的更多相关文章

  1. IOS中通知中心(NSNotificationCenter)

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

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

    一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...

  3. iOS基础 - 通知中心(NSNotificationCenter)

    通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...

  4. iOS中 通知中心Text (实例)

    指定根视图 self.window.rootViewController = [RootViewController new]; 方法实现: #import "RootViewControl ...

  5. IOS Notification 通知中心

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

  6. iOS 通知中心 NSNotificationCenter

    iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...

  7. iOS开发之通知中心(NSNotificationCenter)

    前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...

  8. 通知中心NSNotificationCenter的使用

    通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...

  9. iOS中通知传值

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

随机推荐

  1. UVA 624 (0 1背包 + 打印路径)

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> #i ...

  2. POJ1948Triangular Pastures(DP)

    POJ1948http://poj.org/problem?id=1948 题目大意就是说给你n个木棍让你用它们摆成一个三角形  使得这个三角形的面积最大. 这个题我之前想的时候一直纠结怎么通过之前的 ...

  3. UVa 10004:Bicoloring

    这道题要我们判断所给图是否可以用两种颜色进行染色,即"二染色“.已知所给图一定是强连通图. 分析之: 若图中无回路,则该图是一棵树,一定可以二染色. 若图中有回路,但回路有偶数个节点,仍然可 ...

  4. opencv win7 配置

    按照官网的配置方法,结果出现了下面的错误信息F:\eclipse C++ workspace\test\Debug/../src/test.cpp:18: undefined reference to ...

  5. F5 负载均衡 相关资源

    F5负载均衡之检查命令的说明http://net.zdnet.com.cn/network_security_zone/2010/0505/1730942.shtml F5培训http://wenku ...

  6. 利用HTML5开发Android(7)---HTML5本地存储之Database Storage

    在上一篇<HTML5本地存储之Web Storage篇>中,简单介绍了如何利用localStorage实现本地存储:实际上,除了sessionStorage和localStorage外,H ...

  7. How to use SourceGear DiffMerge in SourceSafe, TFS, and SVN【项目】

    What is DiffMerge DiffMerge is yet-another-diff-and-merge-tool from the fine folks at SourceGear.  I ...

  8. jQuery Deferred(http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. angularjs directive学习心得

    一些常见的错误 在angularjs里,创建directive时,directive的名称应该要使用驼峰式,例如myDirective,而在html里要调用它的时候,就不能用驼峰式了,可以用my-di ...

  10. JavaScript的角色巨变和Web技术的发展

    曾经JavaScript是职业程序员看不上眼的脚本语言,如今只有高级程序员才能驾驭它. JavaScript性质和地位的天翻地覆,正是Web技术飞速变化的印证. 最初职业程序员轻视JavaScript ...