iOS中通知中心NSNotificationCenter应用总结
通知中心(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应用总结的更多相关文章
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- iOS中通知中心(NSNotificationCenter)的使用总结
一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...
- iOS基础 - 通知中心(NSNotificationCenter)
通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...
- iOS中 通知中心Text (实例)
指定根视图 self.window.rootViewController = [RootViewController new]; 方法实现: #import "RootViewControl ...
- IOS Notification 通知中心
1. 通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- 通知中心NSNotificationCenter的使用
通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...
- iOS中通知传值
NSNotification 通知中心传值,可以跨越多个页面传值, 一般也是从后面的页面传给前面的页面. 思路: 第三个界面的值传给第一个界面. 1. 在第一个界面建立一个通知中心, 通过通知中心 ...
随机推荐
- win2008 64位 + oracle11G 64位 IIS7.5 配置WEBSERVICE
第一个错误: 安装过程依旧是那样简单,但在配好IIS站点,准备连接数据库的时候出错了,以下是错误提示:System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更 ...
- Codeforces 711 C. Coloring Trees (dp)
题目链接:http://codeforces.com/problemset/problem/711/C 给你n棵树,m种颜色,k是指定最后的完美值.接下来一行n个数 表示1~n树原本的颜色,0的话就是 ...
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理(转载)
简介: 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomcat 如何分发请求.如何处理多用户同时请求,还有它的 ...
- Unity3d:使用uWebKit插件嵌入网页,网页中的flv视频无法播放
问题描述:unity3d程序,使用uWebKit插件嵌入网页,用来播放FLV视频,有的电脑可以正常播放,有的电脑在网页中播放不了ps:网页中的播放器用的是player.swf解决方案:是由于网页中的播 ...
- wchar_t 和 char 之间转换
vc++2005以后,Visual studio 编译器默认的字符集为Unicode.VC中很多字符处理默认为宽字符wchar_t,如CString的getBuffer(),而一些具体操作函数的输入却 ...
- (C++)String的用法
(转自http://www.cnblogs.com/yxnchinahlj/archive/2011/02/12/1952550.html) 之所以抛弃char*的字符串而选用C++标准程序库中的st ...
- InteractivePNG
在as3中很多时候需要只能选中png中可视区域,即透明区域“感觉可以穿透”.
- 5.迪米特法则(Law Of Demeter)
1.定义 狭义的迪米特法则定义:也叫最少知识原则(LKP,Least Knowledge Principle).如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中的一个类需要 ...
- 学习C++的一些问题总结
C++ 问题 (一) int main() { int i,j,m,n; i=8; j=10; m=++i+j++; //++i是先递加再使用,j++是先使用再递加,故:9+10=19 n=++i+ ...
- codechef Jewels and Stones 题解
Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...