iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知。
在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification。
NSNotification 可以理解为消息对象,包含三个成员变量,如下:
@property (readonly, copy) NSString *name;
@property (nullable, readonly, retain) id object;
@property (nullable, readonly, copy) NSDictionary *userInfo;
name:通知的名称
object:针对某一个对象的通知
userInfo:字典类型,主要是用来传递参数
创建并初始化一个NSNotification可以使用下面的方法:
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;
NSNotificationCenter 通知中心,是一个单例。只能使用 [NSNotificationCenter defaultCenter] 获取通知中心对象。
发送通知可以使用下面的方法:
[[NSNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil];
或者:
NSDictionary *userInfo = @{@"name":@"John",@"age":@""};
NSNotification *notice = [NSNotification notificationWithName:@"testNotification" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notice];
添加一个观察者的方法(可以理解成在某个观察者中注册通知):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNoti:) name:@"testNotification" object:nil];
有四个参数:第一个参数是观察者,也就是谁注册这个通知;第二个参数是当接收到通知时的回调函数;第三个参数是通知的名称,通知名是区分各个通知的唯一标识;第四个参数是接收哪个对象发过来的通知,如果为nil,则接收任何对象发过来的该通知。
移除观察者,两种方法:
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
NSNotificationCenter 的使用举例如下:
发送通知(两种方法):
- (void)btnClicked
{
//[[NSNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:nil]; NSDictionary *userInfo = @{@"name":@"John",@"age":@""}; NSNotification *notice = [NSNotification notificationWithName:@"testNotification" object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notice]; }
增加观察者:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(haveNoti:) name:@"testNotification" object:nil];
回调函数(可以利用回调函数的参数 notification获得该通知的信息,比如name、object、参数):
- (void)haveNoti:(NSNotification *)notification
{
NSLog(@"name = %@",[notification name]); NSDictionary *userInfo = [notification userInfo];
NSLog(@"info = %@",userInfo);
}
针对某一个特定对象的通知的使用场景:
UITextField,比如说我们要监听UITextField 里面内容的变化,此时可以注册下面的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchBarTextChange) name:UITextFieldTextDidChangeNotification object:self.searchBar];
self.searchBar 是 UITextField 类型。
iOS 通知中心 NSNotificationCenter的更多相关文章
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- iOS基础 - 通知中心(NSNotificationCenter)
通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...
- IOS中通知中心(NSNotificationCenter)
摘要 NSNotification是IOS中一个调度消息通知的类,采用单例模式设计,在程序中实现传值.回调等地方应用很广. IOS中通知中心NSNotificationCenter应用总结 一.了 ...
- iOS中通知中心NSNotificationCenter应用总结
通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制.把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象.这句话其实已经很明显的告诉我们要如何使用通知了.第 ...
- iOS通知中心
iOS通知中心 它是iOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信. 当通知中心接受到消息后会 ...
- 通知中心NSNotificationCenter的使用
通知中心NSNotificationCenter的使用 Cocoa框架中,通知中心以及KVO都属于设计模式中的观察者. Source 在使用通知中心之前,对通知中心类进行了简单的封装,以便可读性更强. ...
- iOS中通知中心(NSNotificationCenter)的使用总结
一.了解几个相关的类 1.NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量. 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象. @property (readon ...
- iOS通知中心升级 -可设置按优先级执行block
简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...
- iOS 通知中心扩展制作初步-b
涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...
随机推荐
- java实现DES算法
import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypt ...
- [HUD 1195] Open the Lock
Open the Lock Problem Description Now an emergent task for you is to open a password lock. The passw ...
- 在linux设置环境变量
1.直接用export命令:#export PATH=$PATH:/opt/au1200_rm/build_tools/bin查看是否已经设好,可用命令export查看: [root@localhos ...
- 自动FTP的小脚本
自动FTP的小脚本 使用以下脚本,可以实现自动FTP,将你需要的文件传送到需要的地方,或者将需要的文件从某个地方抓取下来. cd /PATH_YOU_WANT_TO_UPLOAD(DOWNLOAD) ...
- poco网络库分析,教你如何学习使用开源库
Poco::Net库中有 FTPClient HTML HTTP HTTPClient HTTPServer ICMP Logging Mail Messages NetCore NTP OAuth ...
- wifi详解(二)
1 Wifi模块解析和启动流程 1.1 框架分析 WIFI整体框架如图所示: 首先,用户程序使用WifiManager类来管理Wifi模块,它能够获得Wifi模块的状态,配置和 ...
- 关于join算法的四篇文章
MySQL Join算法与调优白皮书(一) MySQL Join算法与调优白皮书(二) MySQL Join算法与调优白皮书(三) MySQL Join算法与调优白皮书(四) MariaDB Join ...
- MySQL row模式binlog复制原理
http://www.360doc.com/content/14/1107/14/12904276_423333021.shtml
- hdu 1573 x问题(中国剩余定理)HDU 2007-1 Programming Contest
只是套模板而已(模板其实也不懂). 留着以后好好学的时候再改吧. 题意—— X = a[i] MOD b[i]; 已知a[i],b[i],求在[1, n]中存在多少x满足条件. 输入—— 第一行一个整 ...
- [Everyday Mathematics]20150108
设 $f$ 在 $(a,b)$ 上 $n+1$ 次可导, 且 $$\bex \ln\frac{f(b)+f'(b)+\cdots+f^{(n)}(b)}{f(a)+f'(a)+\cdots+f^{(n ...