IOS 消息机制(NSNotificationCenter)
来源:互联网 发布:成都知美术馆 编辑:程序博客网 时间:2024/06/05 12:39
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];
本文首发于,博客园,请搜索:博客园 - 寻自己,查看原版文章
本文首发地址:IOS 消息机制(NSNotificationCenter) - http://www.cnblogs.com/xunziji/p/3257447.html
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- IOS 消息机制(NSNotificationCenter)
- 【IOS】消息通信机制NSNotificationCenter
- ios NSNotificationCenter消息通信机制
- IOS学习--NSNotificationCenter消息机制
- ios NSNotificationCenter消息通讯机制
- sub技巧
- 字符串编程问题:求连续出现最长字符串问题(js实现)
- 黑马程序员----类和对象的一点理解
- 使用STM32的systick定时器中断实现RTC工作过程出错
- 抽象工厂模式
- IOS 消息机制(NSNotificationCenter)
- 线程属性总结
- ftk学习记(button篇)
- hdu 1846 Brave Game(巴什博弈)
- SimpleFormController活动图
- 建造者模式
- 工厂模式和抽象工厂模式的区别
- 科目三中模拟灯光使用考试常见的错误 广州学车网光大国际驾校学车
- DataTable操作