IOS开发数据传递机制之NSNotificationCenter

本文详细探讨了Objective-C与Swift两种语言在iOS开发领域的应用与区别,包括其核心特性、优缺点及实际案例分析。
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方法注销注册。这一点需要注意!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值