delegate,block,notification三者的运用

网上对于delegate,block,notification运用都有介绍,但都是介绍的用法;我这里针对于懒人和一些基础不太好的同学进一步补充介绍(是小demo实战)!

delegate是经典设计模式也就是大部分的语言都可以实现的模式,delegate只是保存了一个对象指针,直接回调,没有额外消耗。

block出栈需要将使用的数据从栈拷贝到堆,当然对象的话就是加计数,使用完或者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;
@end
@protocol TestViewDelegate
-(void)selectedString:(NSString *)string;
@end

2,外部写好delegate在调用,在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; 点击按钮跳转到(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,跳转到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 循环缓存
vc.testViewBlock=^(NSString *string){
[weakSelf.navigationController popViewControllerAnimated:YES];//返回上个页面
NSLog(@”Block—>%@”,string);
};
}
打印:Block—>T - T

notification(通知)用法:

1,在vc(SimonViewController)的viewWillAppear添加通知,为了避免重复添加我这里执行了先移除再添加:

-(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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值