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. 在第一个界面建立一个通知中心, 通过通知中心 ...
随机推荐
- 关于WebPlayer Sandbox的小节
不可以像其他build target一样读写I/O 不可以call一些private或者internal methord 只要在一个top level的domain下可以不需要xml dmain po ...
- winForm开发
http://www.cnblogs.com/wuhuacong/p/3199829.html http://www.cnblogs.com/peterzb/archive/2009/06/30/15 ...
- IOS开发之路四(UITabBarController)
前两天看了看斯坦福大学的iphone开发公开课,讲的倒是不错,可看的我云里雾里的,不怎么讲基础和原理,不太适合初学者.今天看了一上午ios5基础教程这本书感觉有点头绪了....废话少说,讲一讲我上午做 ...
- Vehicle’s communication protocol
http://www.crecorder.com/techInfo/commuProtocols.jsp COMMUNICATION PROTOCOLS A “communication protoc ...
- 给eclipse装一些插件
最近换了一个环境,需要折腾一个新的eclipse配置,所以在这里记录下. 1.安装marketplace help=>install,输入地址:http://download.eclipse.o ...
- 气泡形提示控件grumble.js
grumble.js 是一个很特别的气泡形状提示控件,最开始是为 Huddle.com 网站开发的, 它没有通常的north/east/south/west的定位限制. 任何一个grumble都可以放 ...
- 反射遍历List<>泛型类型
有这样一个需求:一个实体模型,有一个属性是一个实体列表List<OrderDetail>,我需要对实体列表遍历,对每一个实体的属性根据属性特性进行验证,如是否为必填等等,如下一个反射方法能 ...
- Android 开发之异常处理篇(一):SDK Manager 闪退的解决方法
这个问题困扰了我很久,之前没解决,就放一放.后来我又专门拿了一个下午来找解决方法,终于搞定! 我的解决方法是修改 android.bat,直接指定java.exe所在位置,不用去调用find_java ...
- Android Compatibility package 兼容性开发套件
我们认为Android 3.0平板电脑操作系统在美国时间2011年2月22日的正式推出,对于Android手机应用程序开发者所象征的意涵是: 之前大家所开发过的Android手机应用,除了可以在And ...
- 【JavsScript】当 JavaScript 从入门到提高前需要注意的细节:变量部分
在javaScript中变量使用var声明的变量是当前作用域的变量,不使用var声明的则肯定是全局变量. http://msdn.microsoft.com/zh-cn/library/dn64545 ...