Block回调和传值(干货)

时间:2022-03-09 20:43:30

如果你还在苦恼block的传值和回调,不妨看看这个Demo,自己整理的,希望对大家有帮助,这是下载地址 https://github.com/ShaoWenLe/BlockTestByValueAndCall-back.git

用的是storyboard结合Xib,如果看着不习惯,可以从上面链接下载源码

//第一界面


//  ViewController.h
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;

@end


//
//  ViewController.m
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import "ViewController.h"
#import "TwoViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _label.text = @"随便";
}
- (IBAction)next:(id)sender {
    //用blockSelf修饰blockSelf.label 避免block块内部_label被循环引用
    __weak ViewController *blockSelf = self;
    
    TwoViewController *twoVC = [[TwoViewController alloc] init];
    //block返回值(跟代理写法挺类似的,就是语法不同,代理的话是此处是self.delegate=self;)
    [twoVC getValue:^(NSString *stringValue) {
        NSLog(@"打印block传的数值:%@",stringValue);
        blockSelf.label.text = stringValue;
    }];
    [self.navigationController pushViewController:twoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//第二界面
//
//  TwoViewController.h
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import <UIKit/UIKit.h>
//重新定义block类名 void返回值类型 BlockValue类名(重命名类名) NSString *stringValue参数
typedef void(^BlockValue)(NSString *stringValue);
@interface TwoViewController : UIViewController
//block属性  此处要用copy修饰
@property (nonatomic, copy) BlockValue blockValue;
@property (weak, nonatomic) IBOutlet UITextField *textField;

- (void)getValue:(BlockValue)aBlock;
@end


//
//  TwoViewController.m
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import "TwoViewController.h"
#import "BlockDataHandle.h"
@interface TwoViewController ()

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.
}

- (void)getValue:(BlockValue)aBlock
{
    self.blockValue = aBlock;
}

//回调button
- (IBAction)callback:(id)sender {
    BlockDataHandle *blockdataHandle = [[BlockDataHandle alloc] init];
    //回调blockdataHandle,传进去@"123",出来的string
    [blockdataHandle getData:@"123" block:^(NSString *string) {
        NSLog(@"打印回调之后的数据%@",string);
    }];
    
}
//返回按钮
- (IBAction)back:(id)sender {
    //判断是否执行setBlock方法,然后再执行里面的操作
    if (self.blockValue) {
        self.blockValue(_textField.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


//回调数据处理类
//
//  blockData.h
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import <Foundation/Foundation.h>
typedef void(^BlockData)(NSString *string);
@interface BlockDataHandle : NSObject
@property (nonatomic, copy) BlockData blockData;
//set方法
-(void)setBlockData:(BlockData)blockData;
//传入参数NSString 
- (void)getData:(NSString *)string block:(BlockData)block;
@end


//
//  blockData.m
//  BlockTestByValueAndCall-back
//
//  Created by 出神入化 on 15/8/15.
//  Copyright (c) 2015年 出神入化. All rights reserved.
//

#import "BlockDataHandle.h"

@implementation BlockDataHandle
-(void)setBlockData:(BlockData)blockData
{
    _blockData = blockData;
}


- (void)getData:(NSString *)string block:(BlockData)block
{
        NSString *str = [NSString stringWithFormat:@"ASDFGH%@", string];
    //调用block
     block(str);
}
@end