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 ...
随机推荐
- BZOJ2298: [HAOI2011]problem a
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2298 题解:刚开始思考的方向错了...一直在想LIS什么的,又发现不合法的情况不好判断,真是个 ...
- Java [Leetcode 122]Best Time to Buy and Sell Stock II
题目描述: Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- Java [Leetcode 235]Lowest Common Ancestor of a Binary Search Tree
题目描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...
- poj 1780 Code
//题目描述:KEY公司开发出一种新的保险箱.要打开保险箱,不需要钥匙,但需要输入一个正确的.由n位数字组成的编码.这种保险箱有几种类型,从给小孩子玩的玩具(2位数字编码)到军用型的保险箱(6位数字编 ...
- 常见MyEclipse报错—— serialVersionUID的作用
先挖好坑 http://swiftlet.net/archives/1268
- 【Unity3D】自动寻路(Nav Mesh Agent组件)
1.首先添加场景模型 2.为场景模型(寻路路径)添加NavMesh渲染,操作:Windows->Navigation->勾选Navigation Static选项->不勾选Gener ...
- Hibernate管理Session和批量操作
Hibernate管理Session Hibernate自身提供了三种管理Session对象的方法 Session对象的生命周期与本地线程绑定 Session对象的生命周期与JTA事务绑定 Hiber ...
- esd-ESD试题
ylbtech-doc:esd-ESD试题 ESD试题 1.A,ESD试题返回顶部 不定项选择题(下列选择题ABCD四项中至少有一项是正确的,共20小题): 1.{ESD题目}储备阶段的几个主要岗位是 ...
- kettle实现文本文件数据抽取方法
KETTLE做调度的思路是,把一个有特定格式的的文本文件,写入ORACLE数据库表, 具体方法见如下操作: 首先来看下文本文件的内容: 1|test1 2|test2 3|test3 通过|进行分割的 ...