关于NSNotification的使用(重点掌握)
2016-01-14 15:20
288 查看
使用消息机制的步骤:
1. 观察者注册消息通知
notificationObserver 观察者 : self
notificationSelector 处理消息的方法名: getUserProfileSuccess
notificationName 消息通知的名字: Notification_GetUserProfileSuccess
notificationSender 消息发送者 : 表示接收哪个发送者的通知,如果第四个参数为nil,接收所有发送者的通知
2. 发送消息通知
//UserProfile Is A Model
//@interface UserProfile : NSObject
notificationName 消息通知的名字: Notification_GetUserProfileSuccess
notificationSender 消息发送者: userProfile
3. 观察者处理消息
NSNotification 接受到的消息信息,主要含:
Name: 消息名称 Notification_GetUserProfileSuccess
object: 消息发送者 userProfile
userInfo: 消息传递的数据信息
4. 观察者注销,移除消息观察者
虽然在 IOS 用上 ARC 后,不显示移除 NSNotification Observer 也不会出错,但是这是一个很不好的习惯,不利于性能和内存。
注销观察者有2个方法:
a. 最优的方法,在 UIViewController.m 中:
If you see the method you don't need to call [super dealloc]; here, only the method without super dealloc needed.
b. 单个移除:
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
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: 消息传递的数据信息
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];
相关文章推荐
- RAC 集群更换IP
- 配置远程控制
- C#高级编程 反射 代码示例
- thinkCMF+iis6实现伪静态, 强烈支持我大TP框架!希望更多人能喜欢它!
- 安装HTTP压力性能测试工具ABSender
- Maven多个mudule只编译、打包指定module
- 证书创建工具 和 参数详解 (Makecert.exe)
- 自定义 toast
- 后台开发面试经验
- 扣丁学堂——Tween和Frame动画
- Linux find 等 查找文件
- Golang初级系列教程-结构体匿名字段-Anonymous fields in structs
- 小丁带你走进git的世界四-重写历史记录
- C# 新委托使用方法 Lambda表达式,运算符 =>
- 在QML应用中显示image tag
- Codeforces Round 338 C
- UIWebview 和加载的圈
- 在chrome浏览器中,防止input[text]和textarea在聚焦时出现黄色边框的解决方法
- StringUtils 常用方法介绍
- Linux 磁盘分区