iOS NSNotificationCenter通知中心使用小結(jié)
前言
最近公司組織兩個(gè)星期的新人培訓(xùn),事情安排的滿滿的,周末都沒(méi)有。說(shuō)好的一個(gè)星期一更新的博客中斷了,讓大家久等了,現(xiàn)在培訓(xùn)結(jié)束,終于又可以安安靜靜的做一個(gè)程序員了,好開(kāi)心。。。
一、NSNotification和Delegate的聯(lián)系和區(qū)別
眾所周知,IOS中經(jīng)常會(huì)使用到NSNotification和delegate來(lái)進(jìn)行一些類之間的消息傳遞。言歸正傳,這兩種有什么區(qū)別呢?
NSNotification就是IOS提供的一個(gè)消息中心,由一個(gè)全局的defaultNotification管理應(yīng)用中的消息機(jī)制。通過(guò)公開(kāi)的API可以看出,這里面使用了是一個(gè)觀察者,通過(guò)注冊(cè)addObserver和解除注冊(cè)removeObserver來(lái)實(shí)現(xiàn)消息傳遞。蘋(píng)果文檔特別提出,在類析構(gòu)的時(shí)候,要記得把removeObserver,不然就會(huì)引發(fā)崩潰,所以NSNotifcation的使用是沒(méi)有retain+1的,NSNotification是一對(duì)多的。
至于Delegate,很簡(jiǎn)單,就是通過(guò)增加一個(gè)指針,然后把需要調(diào)用的函數(shù)通過(guò)delegate傳遞到其他類中,來(lái)得很直截了當(dāng)。不需要通過(guò)廣播的形式去實(shí)現(xiàn),但是,delegate的形式只能是一對(duì)一,不能實(shí)現(xiàn)一對(duì)多。
在什么情況下使用Delegate和NSNotifiation呢?
從效率上看Delegate是一個(gè)很輕量級(jí)的,相對(duì)delegate,NSNotification卻是一個(gè)很重量級(jí)的,效率上delegate明顯要比Noticication高。一般情況我們會(huì)這樣使用。
場(chǎng)景一:
A擁有B,然后B中的一些操作需要回調(diào)到A中,這時(shí)候就簡(jiǎn)單的通過(guò)delegate回調(diào)到A。因?yàn)锽是A創(chuàng)建的,B可以很直接的把delegate賦值A(chǔ)。
場(chǎng)景二:
A和B是兩個(gè)不相干的關(guān)系,A不知道B,B也不知道A,那么這時(shí)候如果通過(guò)delegate就沒(méi)辦法做到,會(huì)相對(duì)復(fù)雜。所以可以通過(guò)NSNotifcation去做一些消息傳遞。
所以使用delegate的情況是兩者有直接的關(guān)系,至于一方知道另一方的存在。而NSNotifcation一般是大家不知道對(duì)方的存在,一般是使用跨模塊的時(shí)候使用。在使用的時(shí)候,使用delegate可能需要多寫(xiě)一些delegate去實(shí)現(xiàn),代碼量比較多。NSNotication只要定義相關(guān)的NotificationName就可以很方便的溝通。兩者各有所長(zhǎng)。
二、監(jiān)聽(tīng)系統(tǒng)自帶的NSNotification
系統(tǒng)里定義了許多的 XxxNotification 名稱,其實(shí)只要 Cmd+Shift+O 打開(kāi) Open Quickly,輸入 NSNotification 或者 UINotification 可以看到許多以 Notification 結(jié)尾的變量定義,由變量名稱也能理解在什么時(shí)候會(huì)激發(fā)什么事件,一般都是向 [NSNotificationCenter defaultCenter] 通知的。

使用步驟
第一步:注冊(cè)系統(tǒng)監(jiān)聽(tīng)事件
//在NSNotificationCenter中注冊(cè)鍵盤(pán)彈出事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardUpEvent:) name:UIKeyboardDidShowNotification object:nil]; //在NSNotificationCenter中注冊(cè)鍵盤(pán)隱藏事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDownEvent:) name:UIKeyboardDidHideNotification object:nil]; //在NSNotificationCenter中注冊(cè)程序從后臺(tái)喚醒事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(becomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
第二步:事件觸發(fā)后的處理
/**
* 彈出鍵盤(pán)事件觸發(fā)處理
*
* @param notification
*/
-(void)keyboardUpEvent : (NSNotification *)notification{
//NSLog(@"鍵盤(pán)彈出事件觸發(fā)==%@",notification);
NSLog(@"鍵盤(pán)彈出事件觸發(fā)");
}
/**
* 鍵盤(pán)隱藏事件觸發(fā)處理
*
* @param notification
*/
-(void)keyboardDownEvent : (NSNotification *)notification{
//NSLog(@"鍵盤(pán)隱藏事件觸發(fā)==%@",notification);
NSLog(@"鍵盤(pán)隱藏事件觸發(fā)");
}
/**
* 程序從后臺(tái)喚醒觸發(fā)處理
*
* @param notification
*/
-(void)becomeActive: (NSNotification *)notification{
NSLog(@"程序從后臺(tái)喚醒觸發(fā)處理");
}
第三步、在dealloc中解除監(jiān)聽(tīng)
/**
*NSNotificationCenter 注意點(diǎn):每一次在接受者對(duì)象中需要delleac把它銷毀掉。
*/
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
三、自定義NSNotification
這里我使用的一個(gè)實(shí)例為:在ViewController中定義一個(gè)按鈕,點(diǎn)擊該按鈕,同時(shí)改變兩個(gè)自定義View中的內(nèi)容。
使用步驟
第一步、在ViewController中生成一個(gè)按鈕,兩個(gè)自定義View
UIButton *postMsgBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 40)]; [postMsgBtn setTitle:@"發(fā)送消息" forState:UIControlStateNormal]; postMsgBtn.backgroundColor = [UIColor grayColor]; [postMsgBtn addTarget:self action:@selector(postMsg:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:postMsgBtn]; MyView *view = [[MyView alloc] initWithFrame:CGRectMake(50, 250, 100, 50)]; [self.view addSubview:view]; MyView *view2 = [[MyView alloc] initWithFrame:CGRectMake(50, 320, 100, 50)]; [self.view addSubview:view2];
第二步、點(diǎn)擊按鈕,發(fā)送Notification
-(void)postMsg: (UIButton *)btn{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_MESSAGE_NAME object:nil userInfo:@{@"msg":@"jingming1"}];
}
第三步、在自定義View中注冊(cè)監(jiān)聽(tīng)事件
第四步、處理監(jiān)聽(tīng)事件
-(void)acceptMsg : (NSNotification *)notification{
NSLog(@"%@",notification);
NSDictionary *userInfo = notification.userInfo;
_label.text = [userInfo objectForKey:@"msg"];
}
第五步、在dealloc中解除監(jiān)聽(tīng)
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開(kāi)發(fā)中使用UILabel設(shè)置字體的相關(guān)技巧小結(jié)
這篇文章主要介紹了iOS開(kāi)發(fā)中UILabel設(shè)置字體的相關(guān)技巧小結(jié),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
實(shí)例分析IOS實(shí)現(xiàn)自動(dòng)打包
本篇文章給大家分享了IOS實(shí)現(xiàn)自動(dòng)打包的相關(guān)知識(shí)點(diǎn),以及需要的操作內(nèi)容做了分享,有需要的朋友可以學(xué)習(xí)下。2018-05-05
KVO實(shí)現(xiàn)自定義文件復(fù)制進(jìn)度效果
這篇文章主要為大家詳細(xì)介紹了KVO實(shí)現(xiàn)自定義文件復(fù)制進(jìn)度效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
iOS中如何使用iconfont圖標(biāo)實(shí)例詳解
iconfont大家在開(kāi)發(fā)中應(yīng)該會(huì)經(jīng)常用到,下面這篇文章主要給大家介紹了在iOS中如何使用iconfont圖標(biāo)實(shí)例的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
iOS統(tǒng)計(jì)項(xiàng)目的代碼總行數(shù)
最近一個(gè)項(xiàng)目有段時(shí)間了,不知道怎樣可以統(tǒng)計(jì)出寫(xiě)了多少行代碼,如何處理這個(gè)問(wèn)題呢,下面我們來(lái)探討下。2015-06-06

