消息机制 NSNotificationCenter 一直都在频繁使用,但是却对其原理不是十分了解。今天就花些时间,把消息机制原理重头到尾好好过一遍。
iOS 提供了一种 "同步的" 消息通知机制,观察者只要向消息中心注册, 即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以互相一无所知,完全解耦。
这种消息通知机制可以应用于任意时间和任何对象,观察者可以有多个,所以消息具有广播的性质,只是需要注意的是,观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,这种消息广播机制是典型的“Observer”模式。
这个要求其实也很容易实现. 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏. 对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做 observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). center将该notifications转发给所有注册对该notification感兴趣的对象. 我们把这些发送notification的对象叫做 poster
消息机制常常用于在向服务器端请求数据或者提交数据的场景,在和服务器端成功交互后,需要处理服务器端返回的数据,或发送响应消息等,就需要用到消息机制。
本文禁止任何网站转载,严厉谴责那些蛀虫们。
本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
本文首发地址:IOS 消息机制(NSNotificationCenter) - http://www.cnblogs.com/xunziji/p/3257447.html
使用消息机制的步骤:

1. 观察者注册消息通知

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

notificationObserver 观察者 : self
notificationSelector 处理消息的方法名: getUserProfileSuccess
notificationName 消息通知的名字: Notification_GetUserProfileSuccess
notificationSender 消息发送者 : 表示接收哪个发送者的通知,如果第四个参数为nil,接收所有发送者的通知

2. 发送消息通知

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

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

notificationName 消息通知的名字: Notification_GetUserProfileSuccess

notificationSender 消息发送者: userProfile

本文禁止任何网站转载,严厉谴责那些蛀虫们。
本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
本文首发地址:IOS 消息机制(NSNotificationCenter) - http://www.cnblogs.com/xunziji/p/3257447.html

3. 观察者处理消息

- (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 消息机制(NSNotificationCenter) - http://www.cnblogs.com/xunziji/p/3257447.html

4. 观察者注销,移除消息观察者

虽然在 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];
本文禁止任何网站转载,严厉谴责那些蛀虫们。
本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
本文首发地址:IOS 消息机制(NSNotificationCenter) - http://www.cnblogs.com/xunziji/p/3257447.html

IOS 消息机制(NSNotificationCenter)的更多相关文章

  1. ios消息机制

    ios消息机制介绍 ios 调用每一个方法的时候其实是走的ios的消息机制 举例介绍一下 创建一个Pserson类 有一个eat 对象方法 那么下面的代码可以用消息机制实现  导入消息头文件    # ...

  2. iOS NSNotificationCenter(消息机制)

    转自:http://blog.csdn.net/liliangchw/article/details/8276803 对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notificatio ...

  3. iOS开发系列--通知与消息机制

    概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...

  4. 消息通信机制NSNotificationCenter -备

    消息通信机制NSNotificationCenter的学习.最近写程序需要用到这类,研究了下,现把成果和 NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为 ...

  5. iOS开发系列--通知与消息机制--转

    来自:http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户 ...

  6. ios--->ios消息机制(NSNotification 和 NSNotificationCenter)

    问题的背景 IOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改 ...

  7. (转)iOS消息推送机制的实现

    原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...

  8. 【iOS】iOS消息推送机制的实现

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

  9. iOS消息推送机制

    iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...

随机推荐

  1. dede织梦cms-dede:autochannel标签

    按排序位置的获取单个栏目的链接信息 >>dede>> {dede:autochannel partsort='' typeid=''}{/dede:autochannel} & ...

  2. MVC Html.BeginForm 与 Ajax.BeginForm 使用总结

    最近采用一边工作一边学习的方式使用MVC5+EF6做一个Demo项目, 期间遇到不少问题, 一直处于研究状态, 没能来得及记录. 今天项目进度告一段落, 得以有空记录学习中遇到的一些问题. 由于MVC ...

  3. 【转】Oracle索引失效问题

    转自:http://www.cnblogs.com/millen/archive/2010/01/18/1650423.html 失效情况分析: <> 单独的>,<,(有时会用 ...

  4. python小程序

    使用python实现在crt中捕捉出现的异常信息,并统计出现的次数: #$language = "Python" #$interface = "1.0" def ...

  5. oracle 导出指定的存储过程

    只能导出以下类型: PROCEDURE PACKAGE PACKAGE BODY TYPE BODY FUNCTION TYPE 也就是,表需要单独导出并导入(已经能俭省很多手动操作了). SET e ...

  6. USER STORIES AND USE CASES - DON’T USE BOTH

    We’re in Orlando for a working session as part of the Core Team building BABOK V3 and over dinner th ...

  7. 前端页面开发,最低兼容IE 8的多设备跨平台问题解决!

    项目要求: 网站能够使用PC.ipad.mobile phone正常访问 页面PSD版式宽度分别为1024px和750px 参考资料 使用CSS3 Media Queries,其作用就是允许添加表达式 ...

  8. BOM DOM Event事件笔记....

    js//获取文件标题 document.body //body document.title //网页标题 document.doctype//文档对象 document.url//路径 //服务器相 ...

  9. webform 组合查询

    界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...

  10. MySQL选择合适的数据类型

    一.char和varchar char是固定长度的,查询速度比varchar速度快的多.char的缺点是浪费存储空间. 检索char列时,返回的结果会删除尾部空格,所以程序需要对为空格进行处理. 对于 ...