NSNotificationCenter应用总结
通常我们在 iOS 中发生什么事件时该做什么是由 Delegate 实现的,例如 View 加载完后会触发 viewDidLoad。 Apple 还为我们提供了另一种通知响应方式,那就是 NSNotification,系统中(UIKeyboardDidShowNotification 等) 以及某些第三方组件(例如 ASIHTTPRequest 的 kReachabilityChangedNotification 等)。
NSNotificationCenter 较之于 Delegate 可以实现更大的跨度的通信机制,可以为两个无引用关系的两个对象进行通信。NSNotificationCenter 的通信原理使用了观察者模式:
2. NSNotificationCenter 在某个时机激发事件(以字符串命名)

@interface classB : NSObject
-(void) testNotification;
@end
@implementation classB
-(void) testNotification{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callback:) name:@"TEST" object:nil];
[[NSNotificationCenter defaultCenter] addObserverForName:@"TEST" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"%@", [note name]);
NSLog(@"%@", [note object]);
NSLog(@"%@", [note userInfo]);
}];
}
-(void) callback:(id)notification{
[self testString];
NSDictionary *info = [notification userInfo];
[info enumerateKeysAndObjectsUsingBlock:
^(id key, id object, BOOL *stop){
//do sth
NSLog(@"%@ = %@", key, object);
}];
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
//test notification
classB *b = [[classB alloc] init];
[b testNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"TEST" object:nil userInfo:@{@"a":@"hello",@"b":@}];
}
return ;
}
运行结果:
-- ::05.589 test2[:] a = hello
-- ::05.589 test2[:] b =
-- ::05.589 test2[:] TEST
-- ::05.589 test2[:] (null)
-- ::05.589 test2[:] {
a = hello;
b = ;
}
NSNotificationCenter应用总结的更多相关文章
- iOS NSNotificationCenter详解
通知中心的特点: 1:同步执行 2: 一对多发送消息 3: 降低程序耦合度 通知中心是单例,目的就是从任意一个发送消息到任意一个接收者,是同步执行的. 那么什么是同步呢? 用网上经典的说法,就是我叫朋 ...
- iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项
我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...
- IOS 消息机制(NSNotificationCenter)
消息机制 NSNotificationCenter 一直都在频繁使用,但是却对其原理不是十分了解.今天就花些时间,把消息机制原理重头到尾好好过一遍. iOS 提供了一种 "同步的" ...
- IOS学习之NSNotificationCenter消息机制
NSNotificationCenter是 Cococa消息中心,统一管理单进程内不同线程的消息通迅. 添加观察者接收通知: //添加通知中心观察者 [[NSNotificationCenter de ...
- 通知中心 NSNotificationCenter 的简单使用方法
NSNotificationCenter(通知中心) [注意]需再dealloc中移除观察者 获取通知中心单例对象 NSNotificationCenter *center=[NSNotifi ...
- Swift利用协议优化NSNotificationCenter
NSNotificationCenter存在的问题 通知没有统一的命名格式 对于通知的命名没有强制的要求,一个项目里可能有多种不同的命名规则.比如: 1 2 3 4 5 6 class Barista ...
- 通知 - NSNotificationCenter
1.每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信: 2.任何一个对象都可以向通知中心发布通知(NSNotification), 描 ...
- 你真的了解NSNotificationCenter吗?
一:首先查看一下关于NSNotificationCenter的定义 @interface NSNotificationCenter : NSObject { @package void * __str ...
- iOS中通知中心(NSNotificationCenter)的使用总结
一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...
随机推荐
- BitCoinJ之Hello World示例程序
简介 比特币(BitCoin)是一种基于区块链(BlockChain)技术的数字化货币. 本文介绍了使用基于Java语言的BitCoinJ API实现一个简单的收取和支付比特币的示例程序. 开发环境 ...
- [函数] Firemonkey iOS 指定目录不要备份到 iCloud
uses iOSapi.Foundation, Macapi.Helpers; // 不要备份到 iCloud by Aone function addSkipBackupAttributeToIte ...
- SQL简单语句总结习题
创建一个表记员工个人信息: --创建一个表 create table plspl_company_info( empno ) not null, ename ) not null, job ), ma ...
- 百度Java研发面经100题 总结
1. 单例模式常见的应用场景分析. 在23种设计模式中,单例模式排行老大.虽然理解简单,但是对于应用场景.你真正的熟悉么?使用单例,是由于没必要每个请求都新建一个对象,这样既浪费CPU又浪费内存:之所 ...
- oracle/MySQL 中的decode的使用
MySQL decode()的等同实现 在Oracle中使用decode方法可以轻松实现代码和值之间的转换,但是在MySQL中该如何实现类似功能呢? MySQL中没有直接的方法可以使用 ...
- 操作系统与c语言
以下是根据看书后的理解做的总结: 最早,unix是使用汇编编写,但是非常简单.后来觉得汇编,换种机器又得重新用另外一种机器汇编重写,太麻烦.于是想设计一种通用的语言,到各种机器上都能运行 当时 ...
- 关于linux下DB2创建数据库报错问题
公司业务需要,把服务搭在中标下,在中标下装了DB2 Express-C v9.7.1,之前用着没有问题,隔了一段时间没用,最近又需要用到它,出了一些菜鸟问题,记录下来以免有人和我犯同样的错误...进入 ...
- nginx性能优化之线程池
默认情况下,nginx的work process按照顺序一个个处理http请求,因此如果后台处理时间较长,则work process会长时间等待IO状态,因此限制并发性.如下所示: 所以,对于可能存在 ...
- SSH免密码登录过程解析和实现
SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议.利用SSH 协议可以有效防止远程管理过程中的信息泄露问题. 从客户端来看,SSH提 ...
- AWS CloudFront CDN直接全站加速折腾记The request could not be satisfied. Bad request
ERROR The request could not be satisfied. Bad request. Generated by cloudfront (CloudFront) Request ...