簡介
網(wǎng)上對于delegate,block,notification運用都有介紹见芹,但都是介紹的用法薯定;我這里針對于懶人和一些基礎不太好的同學進一步補充介紹(是小demo實戰(zhàn))!
delegate是經(jīng)典設計模式也就是大部分的語言都可以實現(xiàn)的模式,delegate只是保存了一個對象指針夏伊,直接回調(diào)抹沪,沒有額外消耗刻肄。
block出棧需要將使用的數(shù)據(jù)從棧拷貝到堆融欧,當然對象的話就是加計數(shù)敏弃,使用完或者block置nil后才消除。所以我們用block時要進行弱引用:ARC下:__weak typeof(self) weakSelf = self;非ARC下:__block typeof(self) weakSelf = self;
notification 通知的用法相對就是比較簡單的噪馏,記茁蟮健:有添加就要有移除;
delegate用法:
1,定義一個vc(TestViewController)欠肾,在你定義的vc.h(TestViewController.h)中進行聲明
@protocol TestViewDelegate;
@interface TestViewController : UIViewController
@property(assign, nonatomic) id <TestViewDelegate> testViewDelegate;
@end
@protocol TestViewDelegate <NSObject>
-(void)selectedString:(NSString *)string;
@end
>
2,外部寫好delegate在調(diào)用隅要,在vc(TestViewController)引用TestViewDetegate.h ,之后和1一樣。
這里寫圖片描述
3董济,就是傳值了步清,在vc(TestViewController)定義一個button在其點擊方法中添加delegate的傳值
- (IBAction)back:(id)sender {
if (self.testViewDelegate && [self.testViewDelegate respondsToSelector:@selector(selectedString:)]) {
[self.testViewDelegate selectedString:@"T - T"];
}
}
4,接收delete的傳值時,在你的第一個vc(SimonViewController)添加TestViewDelegate虏肾,如圖1; 點擊按鈕跳轉(zhuǎn)到(TestViewController)中廓啊,并對delegate賦self , 如圖1下面代碼;
這里寫圖片描述
- (IBAction)buttonClick:(id)sender {
TestViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TestView"];
vc.testViewDelegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
5,接收到delete傳值處理
-(void)selectedString:(NSString *)string{
[self.navigationController popViewControllerAnimated:YES];//返回上個頁面
NSLog(@"string --- >%@",string);
}
打臃夂馈:string --- >T - T
block用法:
1谴轮,block的聲明也是vc.h(TestViewController.h)中進行聲明,如下:
typedef void (^TestViewblock)(NSString *string);
@interface TestViewController : UIViewController
@property(nonatomic,strong)TestViewblock testViewBlock;
@end
2吹埠,在vc(TestViewController)定義一個button在其點擊方法中添加block的傳值
- (IBAction)back:(id)sender {
if (_testViewBlock) {
_testViewBlock(@"T - T");
}
}
3,跳轉(zhuǎn)到vc(TestViewController)及block值處理:
- (IBAction)buttonClick:(id)sender {
-
TestViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TestView"];
[self.navigationController pushViewController:vc animated:YES];
__weak typeof(self) weakSelf=self;//避免block 循環(huán)緩存
vc.testViewBlock=^(NSString *string){
[weakSelf.navigationController popViewControllerAnimated:YES];//返回上個頁面
NSLog(@"Block--->%@",string);
};
}
打拥诓健:Block--->T - T
notification(通知)用法:
1,在vc(SimonViewController)的viewWillAppear添加通知疮装,為了避免重復添加我這里執(zhí)行了先移除再添加:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];//移除通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification:) name:@"test_notification" object:nil];//添加通知
}
2,在vc(TestViewController)點擊進行傳值
- (IBAction)back:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"test_notification" object:@"T - T"];
}
3,在vc(SimonViewController)接收通知處理
-(void)notification:(NSNotification *)notification{
NSString *sting = [notification object];
NSLog(@"sting --->%@",sting);
}
打印:sting --->T - T
4,在vc(SimonViewController)的viewWillDisappear中移除通知粘都,通知移除后廓推,在vc(TestViewController)進行傳值,將不被接收處理翩隧;小伙伴們樊展,可以試試!
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}