IOS 消息机制(NSNotificationCenter)
本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
本文首发地址: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)的更多相关文章
- ios消息机制
ios消息机制介绍 ios 调用每一个方法的时候其实是走的ios的消息机制 举例介绍一下 创建一个Pserson类 有一个eat 对象方法 那么下面的代码可以用消息机制实现 导入消息头文件 # ...
- iOS NSNotificationCenter(消息机制)
转自:http://blog.csdn.net/liliangchw/article/details/8276803 对象之间进行通信最基本的方式就是消息传递,在Cocoa中提供Notificatio ...
- iOS开发系列--通知与消息机制
概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...
- 消息通信机制NSNotificationCenter -备
消息通信机制NSNotificationCenter的学习.最近写程序需要用到这类,研究了下,现把成果和 NSNotificationCenter是专门供程序中不同类间的消息通信而设置的,使用起来极为 ...
- iOS开发系列--通知与消息机制--转
来自:http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户 ...
- ios--->ios消息机制(NSNotification 和 NSNotificationCenter)
问题的背景 IOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改 ...
- (转)iOS消息推送机制的实现
原:http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html iOS消息推送机制的实现 iOS消息推送的工作机制可以简单的用下 ...
- 【iOS】iOS消息推送机制的实现
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...
- iOS消息推送机制
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...
随机推荐
- dede织梦cms-dede:autochannel标签
按排序位置的获取单个栏目的链接信息 >>dede>> {dede:autochannel partsort='' typeid=''}{/dede:autochannel} & ...
- MVC Html.BeginForm 与 Ajax.BeginForm 使用总结
最近采用一边工作一边学习的方式使用MVC5+EF6做一个Demo项目, 期间遇到不少问题, 一直处于研究状态, 没能来得及记录. 今天项目进度告一段落, 得以有空记录学习中遇到的一些问题. 由于MVC ...
- 【转】Oracle索引失效问题
转自:http://www.cnblogs.com/millen/archive/2010/01/18/1650423.html 失效情况分析: <> 单独的>,<,(有时会用 ...
- python小程序
使用python实现在crt中捕捉出现的异常信息,并统计出现的次数: #$language = "Python" #$interface = "1.0" def ...
- oracle 导出指定的存储过程
只能导出以下类型: PROCEDURE PACKAGE PACKAGE BODY TYPE BODY FUNCTION TYPE 也就是,表需要单独导出并导入(已经能俭省很多手动操作了). SET e ...
- 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 ...
- 前端页面开发,最低兼容IE 8的多设备跨平台问题解决!
项目要求: 网站能够使用PC.ipad.mobile phone正常访问 页面PSD版式宽度分别为1024px和750px 参考资料 使用CSS3 Media Queries,其作用就是允许添加表达式 ...
- BOM DOM Event事件笔记....
js//获取文件标题 document.body //body document.title //网页标题 document.doctype//文档对象 document.url//路径 //服务器相 ...
- webform 组合查询
界面: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx ...
- MySQL选择合适的数据类型
一.char和varchar char是固定长度的,查询速度比varchar速度快的多.char的缺点是浪费存储空间. 检索char列时,返回的结果会删除尾部空格,所以程序需要对为空格进行处理. 对于 ...