美文网首页
block逆向传参数 和 delegate 逆向传参数

block逆向传参数 和 delegate 逆向传参数

作者: 片片碎 | 来源:发表于2016-08-24 15:27 被阅读91次

从控制器secondViewController(简称B)传给ViewController(简称A)

总结:block和delegate都可以实现逆向传参数,区别:

delegate的流程是:B定义协议内容   A添加代理并实现B的协议

但block 实现在block里面,A中只要写个B.block赋值

备注:顺向传参:一般使用property参数或重写init

一. Block实现:

------------------------------------------

//控制器ViewController.m

#import"ViewController.h"

#import"secondViewController.h"

@interfaceViewController(){

__weakIBOutletUILabel*m_label1;

__weakIBOutletUILabel*m_label2;

}

- (IBAction)btnAction:(UIButton*)sender {

UIStoryboard*story = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

secondViewController*vc = [storyinstantiateViewControllerWithIdentifier:@"secondVC"];

//添加secondViewController的两个block

vc.mm_block= ^(NSString* name,NSString* age){

//bock回调的参数,传递成功

NSLog(@"---%@----",name);

m_label1.text= [NSStringstringWithFormat:@"%@ age is %@",name,age];

};

vc.sBlock= ^(NSString*m_city){

//bock回调的参数,传递成功

m_label2.text= [NSStringstringWithFormat:@"city: %@",m_city];

NSLog(@"---%@----",m_city);

};

[selfpresentViewController:vcanimated:YEScompletion:nil];

}

------------------------------------------

//控制器secondViewController.h

#import

#import"ViewController.h"

typedefvoid(^cBlock)(NSString*x,NSString*y);//使用typedef可创建多个此block使用

@interfacesecondViewController : UIViewController

@property(nonatomic,strong) cBlock mm_block;//声明block

@property(nonatomic,strong)void(^sBlock)(NSString* city);//声明block

@end

//控制器secondViewController.m

- (IBAction)returnSuperVc:(id)sender {

[self dismissViewControllerAnimated:YES completion:^(){

//判断block是否存在

if (_mm_block) {

_mm_block(@"caicai",@"18"); //赋值 调用block

}

//判断block是否存在

if (_sBlock) {

_sBlock(@"shenzhen");//赋值 调用block

}

}];

}


二. delegate实现

----------

//控制器secondViewController.h

#import

#import"ViewController.h"

//定义协议

@protocolsecondViewDelegate

-(void)secondView:(NSString* )name age:(NSString*)age;

@end

@interfacesecondViewController :UIViewController

@property(nonatomic,weak)id delegate;//声明代理

@end

//控制器secondViewController.m

- (IBAction)returnSuperVc:(id)sender {

[selfdismissViewControllerAnimated:YEScompletion:^(){

//判断代理是否存在

if(_delegate) {

[_delegatesecondView:@"caicai"age:@"18"];//代理传参

}

}];

}

------------------------------------------

//控制器ViewController.h

#import

#import"secondViewController.h"

@interfaceViewController :UIViewController//添加代理

@end

------------------------------------------

//控制器ViewController.m

- (IBAction)btnAction:(UIButton*)sender {

UIStoryboard*story = [UIStoryboardstoryboardWithName:@"Main"bundle:nil];

secondViewController*vc = [storyinstantiateViewControllerWithIdentifier:@"secondVC"];

vc.delegate=self; //添加代理

[selfpresentViewController:vcanimated:YEScompletion:nil];

}

#pragma mark secondViewDelegate

-(void)secondViewDelegate:(NSString* )name age:(NSString*)age{

//delegate回调的参数,传递成功

NSLog(@"---%@----",name);

m_label1.text= [NSStringstringWithFormat:@"%@ age is %@",name,age];

}

相关文章

网友评论

      本文标题:block逆向传参数 和 delegate 逆向传参数

      本文链接:https://www.haomeiwen.com/subject/pphosttx.html