IOS 消息機制(NSNotificationCenter)


消息機制 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM