三者的區(qū)別:通知是一對(duì)多,代理和block是一對(duì)一。
三者的優(yōu)缺點(diǎn):通知:寫法簡(jiǎn)單,但是要注意釋放observer,影響內(nèi)存的消耗,代理:寫法復(fù)雜,有七步,要設(shè)置協(xié)議,但是能夠靈活的添加所需要的方法,引用也方便。block:寫法簡(jiǎn)單,但是要注意循環(huán)引用,調(diào)用方法 weakSelf。
BTW簡(jiǎn)書的頁面真是丑到家。
三者的用法
1.通知
//創(chuàng)建通知發(fā)出者
NSDictionary* dic =@{@"num":_tf.text,@"price":_tf1.text,@"totalPrice":_dealMoney.text};
[[NSNotificationCenterdefaultCenter]postNotificationName:@"numAndePriceChanged"object:niluserInfo:dic];
//接受通知的對(duì)象
-(void)lissionNoti{
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(refreshView:)name:@"numAndePriceChanged"object:nil];
}
-(void)refreshView:(NSNotification*)noti{
NSIndexSet*indexSet=[[NSIndexSetalloc]initWithIndex:[IndexPathintValue]];
NSString* num = noti.userInfo[@"num"];
NSString* price = noti.userInfo[@"price"];
NSString* totalPrice = noti.userInfo[@"totalPrice"];
[ModelsetValue:priceforKey:@"ppPrice"];
[ModelsetValue:totalPriceforKey:@"totalPrice"];
[ModelsetValue:numforKey:@"ppTon"];
[ModelsetValue:totalPriceforKey:@"actualPrice"];
[selfreloadSections:indexSetwithRowAnimation:UITableViewRowAnimationAutomatic];
}
//銷毀接收通知的對(duì)象,優(yōu)化內(nèi)存
-(void)dealloc{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
2.block用法Block是一種比較特殊的數(shù)據(jù)類型,它可以用于兩個(gè)界面質(zhì)檢傳值,也可以對(duì)代碼封裝作為參數(shù)傳遞。block常常結(jié)合typedef來使用(也可以不用,看個(gè)人習(xí)慣),用自己定義的類型去創(chuàng)建block顯得更加的簡(jiǎn)單便捷,接下來舉例實(shí)現(xiàn)一個(gè)block回調(diào),將傳入的參數(shù)加上另一個(gè)值后再回調(diào)回來:
//.h文件-->用來傳值的view
#import
// block格式:返回值(^block名字)(參數(shù))
// (1)定義block
typedefvoid(^myBlcok)(NSArray*array);
@interfaceLiuboRegisterViewController :UIViewController
// (2)申明block屬性
@property(strong,nonatomic)myBlcokblock;
// (3)初始化方法
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil withCompletionBlock:(myBlcok)block;
@end
//.m文件 將要傳的值保存到代碼塊里
- (IBAction)registerButtonClick:(id)sender
{
// (5)回調(diào)block
if(self.userNameTextField.text.length!=0&&self.passwordTextField.text.length!=0&&self.confirmPasswordTextField.text.length!=0&&self.emailTextField.text.length!=0)
{
//裝入數(shù)組里面
NSArray*array = [NSArrayarrayWithObjects:self.userNameTextField.text,self.passwordTextField.text,nil];
if(self.block)
{
self.block(array);
}
[self dismissViewControllerAnimated:YES ?completion:nil];
}
}
//需要得到block里值的.m文件
#pragma mark -注冊(cè)
- (IBAction)registerButtonClick:(id)sender
{
//LiuboRegisterViewController *registerViewController = [[LiuboRegisterViewController alloc] initWithNibName:@"LiuboRegisterViewController" bundle:nil];
// block回調(diào)方法
LiuboRegisterViewController*registerViewController = [[LiuboRegisterViewControlleralloc]initWithNibName:@"LiuboRegisterViewController"bundle:nilwithCompletionBlock:^(NSArray*array)
{
if([arraycount] !=0)
{
self.userNameTextField.text= array[0];
self.passwordTextField.text= array[1];
}
}];
UINavigationController*navigationController = [[UINavigationControlleralloc]initWithRootViewController:registerViewController];
[selfpresentViewController:navigationControlleranimated:YEScompletion:^{
}];
}
3.代理
委托其實(shí)是一種設(shè)計(jì)模式,通俗一點(diǎn)來講就是當(dāng)自己有需求要處理但是不方便的時(shí)候,就建立一個(gè)委托,請(qǐng)別人來幫忙處理。舉個(gè)例子:“我要給路人甲打電話,但是我不知道李斯的電話號(hào)碼;我就拜托章三去查詢,章三查到后就發(fā)短信給了我號(hào)碼”,章三就是我的委托對(duì)象。相信大家在ios開發(fā)中經(jīng)常會(huì)看到類似@protocol(協(xié)議)的代碼吧!如果我們要實(shí)現(xiàn)一個(gè)delegate委托,就先要先定義protocol(協(xié)議),在指定收到回調(diào)的類中(也就是我)去實(shí)現(xiàn)協(xié)議中的函數(shù)(例如收短信),如果沒有實(shí)現(xiàn),編譯器就會(huì)報(bào)警告;下面是一個(gè)簡(jiǎn)單的例子,SecondviewController會(huì)回調(diào)FirstViewController,F(xiàn)irstViewController實(shí)現(xiàn)協(xié)議中的回調(diào)函數(shù)
//.h文件
#import
// (1)創(chuàng)建協(xié)議
@protocolLiuboRegisterViewControllerDelegate
//必須要實(shí)現(xiàn)的協(xié)議方法
//@required
//可選擇性實(shí)現(xiàn)的協(xié)議方法
// (2)申明協(xié)議方法
@optional
- (void)sendVaules:(NSArray*)array;
@end
@interfaceLiuboRegisterViewController :UIViewController
// (3)申明代理
@property(assign,nonatomic)iddelegate;
@end
//.m文件
- (IBAction)registerButtonClick:(id)sender
{
if(self.userNameTextField.text.length!=0&&self.passwordTextField.text.length!=0&&self.confirmPasswordTextField.text.length!=0&&self.emailTextField.text.length!=0)
{
//保存到數(shù)組里面
NSArray*array = [NSArrayarrayWithObjects:self.userNameTextField.text,self.passwordTextField.text,nil];
// (4)回調(diào)協(xié)議方法
if([self.delegaterespondsToSelector:@selector(sendVaules:)])
{
[self.delegatesendVaules:array];
}
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}
}

//.h文件
// (5)添加代理
@interfaceLiuboLoginViewController()<LiuboRegisterViewControllerDelegate>
#pragma mark -注冊(cè)
- (IBAction)registerButtonClick:(id)sender
{
LiuboRegisterViewController*registerViewController = [[LiuboRegisterViewControlleralloc]initWithNibName:@"LiuboRegisterViewController"bundle:nil];
// (6)設(shè)置代理
registerViewController.delegate=self;
UINavigationController*navigationController = [[UINavigationControlleralloc]initWithRootViewController:registerViewController];
[self presentViewController:navigationController animated:YES completion:nil
}
// (7)響應(yīng)回調(diào)的協(xié)議方法
- (void)sendVaules:(NSArray*)array
{
if([arraycount] !=0)
{
NSString*userName = array[0];
NSString*password = array[1];
self.userNameTextField.text= userName;
self.passwordTextField.text= password;
}
}
