NSNotificationCenter官方文档上这么解释的:An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object
is essentially a notification dispatch table.简单解释就是该对象提供了一个在程序内发送数据的一种机制。那么怎么在对象之间发送数据呢?很简单
一、
//先得到一个NSNotificationCenter对象
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册接收器
//name是接收器的名字,这里的名字与发送时的名字一致
//selector方法名,这里定义的方法需要有一个参数(NSNotification *)notification
[center addObserver:self
selector:@selector(方法名)
name:你定义的接收器的名字
object:nil];
//第二步就是发送了
[[NSNotificationCenter defaultCenter]
postNotificationName:你定义的名字 object:传递的对象,可为nil];
这样就完事了,ios中的对象之间传递数据还是比较简单的,主要是人家系统开发者设计的好。还有一些需要注意的地方,addObserver注册了接收器,也需要remove,调用一下removeObserver方法注销注册。这一点需要注意!
一、
//先得到一个NSNotificationCenter对象
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//注册接收器
//name是接收器的名字,这里的名字与发送时的名字一致
//selector方法名,这里定义的方法需要有一个参数(NSNotification *)notification
[center addObserver:self
selector:@selector(方法名)
name:你定义的接收器的名字
object:nil];
//第二步就是发送了
[[NSNotificationCenter defaultCenter]
postNotificationName:你定义的名字 object:传递的对象,可为nil];
这样就完事了,ios中的对象之间传递数据还是比较简单的,主要是人家系统开发者设计的好。还有一些需要注意的地方,addObserver注册了接收器,也需要remove,调用一下removeObserver方法注销注册。这一点需要注意!