區(qū)別
1.NotificationCenter 通知中心:“一對多”真慢,在APP中,很多控制器都需要知道一個事件理茎,應該用通知黑界;
2.delegate 代理委托:
???? 1,“一對一”皂林,對同一個協(xié)議朗鸠,一個對象只能設置一個代理delegate,所以單例對象就不能用代理础倍;
???? 2烛占,代理更注重過程信息的傳輸:比如發(fā)起一個網(wǎng)絡請求,可能想要知道此時請求是否已經(jīng)開始沟启、是否收到了數(shù)據(jù)忆家、數(shù)據(jù)是否已經(jīng)接受完成、數(shù)據(jù)接收失敗
3.block(閉包)
???? block和delegate一樣德迹,一般都是“一對一”之間通信交互芽卿,相比代理block有以下特點
??? 1:寫法更簡練,不需要寫protocol胳搞、函數(shù)等等
??? 2卸例,block注重結(jié)果的傳輸:比如對于一個事件称杨,只想知道成功或者失敗,并不需要知道進行了多少或者額外的一些信息
??? 3筷转,block需要注意防止循環(huán)引用
用法
1.NotificationCenter 通知中心
消息通知機制顧名思義姑原,在IOS開發(fā)中它就是通過消息,來達到通知的目的旦装。我們需要在通知中心注冊我們想要監(jiān)聽的消息页衙,當項目中有地方發(fā)出這個消息的時候,通知中心會發(fā)送給注冊這個消息的對象
a. addObserver
```
- (void)viewDidLoad {
[superviewDidLoad];
//?Do?any?additional?setup?after?loading?the?view,?typically?from?a?nib.
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(getMessageContent:)name:@"Notification_Send_Message"object:nil];
}
```
b.postNotification
```
- (void)sendTheMessage:(NSMutableDictionary*)m_dic{
//????[[NSNotificationCenter?defaultCenter]?postNotificationName:@"Notification_Send_Message"?object:m_dic];
[[NSNotificationCenterdefaultCenter]postNotificationName:@"Notification_Send_Message"object:m_dicuserInfo:nil];
}
```
c.處理消息
```
- (void)getMessageContent:(NSNotification*)notifi{
NSMutableArray*m_array?=?(NSMutableArray*)notifi.object;
NSLog(@"++++++%@++++++",?m_array);
//........
}
```
d.removeObserver
```
- (void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"Notification_Send_Message"object:nil];
}
```
2.delegate 委托
委托其實是一種設計模式阴绢,通俗一點來講就是當自己有需求要處理但是不方便的時候店乐,就建立一個委托,請別人來幫忙處理呻袭。舉個例子:“我要給路人甲打電話眨八,但是我不知道李斯的電話號碼;我就拜托章三去查詢左电,章三查到后就發(fā)短信給了我號碼”廉侧,章三就是我的委托對象。相信大家在ios開發(fā)中經(jīng)常會看到類似
@protocol(協(xié)議)的代碼吧篓足!如果我們要實現(xiàn)一個delegate委托段誊,就先要先定義protocol(協(xié)議),在指定收到回調(diào)的類中(也就是我)去實現(xiàn)協(xié)議中的函數(shù)(例如收短信),如果沒有實現(xiàn)栈拖,編譯器就會報警告连舍;下面是一個簡單的例子,SecondviewController會回調(diào)FirstViewController涩哟,F(xiàn)irstViewController實現(xiàn)協(xié)議中的回調(diào)函數(shù):
協(xié)議:ViewSelectedDelegate
```
#ifndef ViewSelectedDelegate_h
#define?ViewSelectedDelegate_h
@protocolViewSelectedDelegate?
-?(void)viewSelectedAtIndex:(int)index;
@end
#endif?/*?ViewSelectedDelegate_h?*/
```
FirstViewController:
```
- (void)viewDidLoad {
[superviewDidLoad];
//?Do?any?additional?setup?after?loading?the?view?from?its?nib.
self.secondView=?[[SecondViewControlleralloc]initWithNibName:@"SecondViewController"bundle:nil];
self.secondView.m_delegate=self;
}
-?(void)didReceiveMemoryWarning?{
[superdidReceiveMemoryWarning];
//?Dispose?of?any?resources?that?can?be?recreated.
}
-?(void)viewSelectedAtIndex:(int)index{
NSLog(@"select?view?at?index?%d",?index);
}
```
SecondViewController:
```
@interfaceSecondViewController : UIViewController
@property(strong,nonatomic)id?m_delegate;
-?(IBAction)clickTheBtn:(id)sender;
```
```
@implementationSecondViewController
@synthesizem_delegate?=?_m_delegate;
-?(void)viewDidLoad?{
[superviewDidLoad];
//?Do?any?additional?setup?after?loading?the?view?from?its?nib.
}
-?(void)didReceiveMemoryWarning?{
[superdidReceiveMemoryWarning];
//?Dispose?of?any?resources?that?can?be?recreated.
}
-?(IBAction)clickTheBtn:(id)sender?{
[self.m_delegateviewSelectedAtIndex:1];
}
```
3.block(閉包)
Block是一種比較特殊的數(shù)據(jù)類型索赏,它可以用于兩個界面質(zhì)檢傳值痘拆,也可以對代碼封裝作為參數(shù)傳遞介蛉。block常常結(jié)合typedef來使用,用自己定義的類型去創(chuàng)建block顯得更加的簡單便捷卡睦,接下來舉例實現(xiàn)一個block回調(diào)器仗,將傳入的參數(shù)加上另一個值后再回調(diào)回來:
SecondViewController的實現(xiàn):
```
#import
//自定義一個block
typedefvoid(^changeValueBlock)(intvalue);
@interfaceSecondViewController?:?UIViewController
@property(strong,nonatomic)?changeValueBlock?changeBlock;
@property(assign)intoldValue;
-?(void)updateTheValue:(int)?oldValueBlock:(changeValueBlock)block;
-?(IBAction)clickTheBtn:(id)sender;
@end
```
FirstviewController的實現(xiàn):
```
- (void)viewDidLoad {
[superviewDidLoad];
//?Do?any?additional?setup?after?loading?the?view?from?its?nib.
self.secondView=?[[SecondViewControlleralloc]initWithNibName:@"SecondViewController"bundle:nil];
[self.secondViewupdateTheValue:10Block:^(intvalue)?{
//回調(diào)
NSLog(@"received?the?new?value:%d",?value);
}];
}
```