ww笔记-iOS之用Block替代代理传回push之后的值

如果两个试图只是简单的1对1传值的话可以用Block代替delegate,使用简单。

在Viewcontroller push/present BlockViewController 之后,可向Viewcontroller传回BlockViewController的某个Value ,
有两种方式:
1.可在present/push之后直接传回
2.可在push之后,Viewcontroller中某个按钮触发时传回
如以下demo

Viewcontroller.m
#import "ViewController.h"
#import "BlockViewController.h"
@interface ViewController (){
    BlockViewController * VC;
    UILabel * _testLabel;

}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	//接收回传值
    _testLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 40)];
    _testLabel.text=@"label";
    [self.view addSubview:_testLabel];
    
    //点击后更改_testLabel.text
     UIButton * touchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [touchBtn setBackgroundColor:[UIColor orangeColor]];
    [touchBtn setFrame:CGRectMake(
                                     (self.view.frame.size.width-50)/2,
                                     (self.view.frame.size.height-50)/2,
                                     50,
                                     50)];
    [touchBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:touchBtn];


    UIButton * pushBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [pushBtn setBackgroundColor:[UIColor orangeColor]];
    [pushBtn setFrame:CGRectMake(
                                     (self.view.frame.size.width-50)/2,
                                     (self.view.frame.size.height-50)/2+100,
                                     50,
                                     50)];
    [pushBtn setTitle:@"push" forState:UIControlStateNormal];
    [pushBtn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:pushBtn];


}
-(void)btnClick{
    /****************
     可在点击某个按钮之后设置,_testLabel.text
     ***************/
    [self setViewController];


}
-(void)push{
    VC=[[BlockViewController alloc]init];
    [self presentViewController:VC animated:YES completion:^{
        /****************
         可在present/push之后设置,_testLabel.text
         ***************/
        //[self setViewController];
    }];
}
//设置_testLabel.text
-(void)setViewController{
//1.调用方法回传
    [VC returnName:^(NSString *str) {
        _testLabel.text=[NSString stringWithFormat:@"%@",str];
        
    }];
//2.直接回传
//_testLabel.text=VC.block;
    
}


@end



BlockViewController.h
#import <UIKit/UIKit.h>
typedef void (^myVCBlock) (NSString * str);
@interface BlockViewController : UIViewController
-(void)returnName:(myVCBlock)block;
@property(nonatomic,assign)myVCBlock block;


@end

BlockViewController.m
@synthesize block;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIButton * viewBackBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [viewBackBtn setBackgroundColor:[UIColor orangeColor]];
    [viewBackBtn setFrame:CGRectMake(
                                     (self.view.frame.size.width-50)/2,
                                     (self.view.frame.size.height-50)/2,
                                     50,
                                     50)];
    [viewBackBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:viewBackBtn];


}
//回传
-(void)returnName:(myVCBlock)block{
    block([self setValue]);
}
//设置回传值
-(NSString *)setValue{
    return @"你好,这是一个回传值";
}
-(void)btnClick{

   //也可直接回传或者通过-(void)returnName:(myVCBlock)block方法回传
   //block=(myVCBlock)([self setValue]);
   
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值