NSNotificationCenter --- 消息机制

~~~~我的生活,我的点点滴滴!!


参考链接 http://www.cnblogs.com/xunziji/p/3257447.html




官方提供的原理图:





今天主角主要为两个类:


1、NSNotificationCenter

NSNotificationCenter是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,

通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一

直存在于一个应用的生命周期。


2、NSNotification

这是消息携带的载体,通过它,可以把消息内容传递给观察者。


3、一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收

通知。


通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。

注意当接受到消息后,不想再收到消息了,要把observer删除remove。


     最近做了一个简单的项目中我用到数据传递与回传,有一个中间controller界面的显示,想好好传参实在是麻烦,并且数据量也不是特别大,

故想到iOS的消息中心来传,这样既能解藕程序,逻辑也不麻烦。



消息机制 NSNotificationCenter 一直都在频繁使用,但是却对其原理不是十分了解,今天就花些时间,把消息机制原理重头到尾好好过一遍。


     iOS提供了一种 "同步的" 消息通知机制,观察者只要向消息中心注册,即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以

互相一无所知,完全解耦。这种消息通知机制可以应用于任意时间和任何对象,观察者可以有多个,所有消息具有广播的性质,只是需要注意的是,

观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,这种消息广播机制是典型的“Observer”模式。这个要求其实也很容易实现,

每个运行中的application都有一个NSNotificationCenter的成员变量,对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我),

我们把这些注册对象叫做observer,其它的一些对象会给center发送notifications(我捡到了一只小狗),center将该notifications转发给所有注册对

该notification感兴趣的对象,我们把这些发送notification的对象叫做 poster,消息机制常常用于在向服务器端请求数据或者提交数据的场景,在

和服务器端成功交互后,需要处理服务器端返回的数据,或发送响应消息等,就需要用到消息机制。



使用消息机制的步骤:


一、观察者注册消息通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getUserProfileSuccess:) name:@"Notification_GetUserProfileSuccess" object:nil];

notificationObserver 观察者 : self

notificationSelector 处理消息的方法名: getUserProfileSuccess 

notificationName 消息通知的名字: Notification_GetUserProfileSuccess

notificationSender 消息发送者 : 表示接收哪个发送者的通知,如果第四个参数为nil,接收所有发送者的通知


 

二、发送消息通知


/*
UserProfile Is A Model
@interface UserProfile : NSObject
*/

[[NSNotificationCenter defaultCenter] postNotificationName:@"Notification_GetUserProfileSuccess" object:userProfile userInfo:nil];

notificationName 消息通知的名字: Notification_GetUserProfileSuccess

notificationSender 消息发送者: userProfile


 


三、观察者处理消息 

- (void) getUserProfileSuccess: (NSNotification*) aNotification
{
	self.userProfile = [aNotification object];


	lblName.text = self.userProfile.Name;
	lblEENO.text = self.userProfile.EENO;
	lblNric.text = self.userProfile.NRIC;
	lblBirthday.text =self.userProfile.Birthday;
	lblHireDate.text = self.userProfile.Hiredate;


	txtMobilePhone.text = self.userProfile.Mobile;
	txtEmail.text = self.userProfile.Email;
}


NSNotification 接受到的消息信息,主要含:

Name: 消息名称 Notification_GetUserProfileSuccess

object: 消息发送者 userProfile

userInfo: 消息传递的数据信息


 

四、观察者注销,移除消息观察者

虽然在 iOS 用上 ARC 后,不显示移除 NSNotification Observer 也不会出错,但是这是一个很不好的习惯,不利于性能和内存。

注销观察者有2个方法:


a、最优的方法,在 UIViewController.m 中:

-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//If you see the method you don't need to call [super dealloc]; here, only the method without super dealloc needed.



b、单个移除:


[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Notification_GetUserProfileSuccess" object:nil];


可以直接在上面第三点“处理消息”函数中移除,即做到用完就释放。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值