Block的一些定义和使用

在开发中传值常常会用到代理和Block,今天先说说Block的使用:

1.Block 的定义:

格式: 返回值/Block名称/参数
举例:

void (^myBlock)(UIImageView *)

2.定义Block中要执行的方法
举例:

    = ^(UIImageView *imageView){
  
    NSLog(@"----------");
};

3.执行一个Block

举例:

myBlock(self.imageView);

4.传值事例:

情况说明: 有A、B两个界面,A界面有一个展示内容的UILabel,B界面有一个可以输入文字的UITextField.当从A页面PUSH到B页面的时候,在B页面中的UITextField中输入内容后,从B页面返回到A界面的时候,在A页面中的UILabel 中可以看到在B页面中输入的内容。

A页面 对应 OneViewController 类
B页面 对应 SecondViewController 类

1.需要先在B界面中定义Block:
SecondViewController.h 中:

#import <UIKit/UIKit.h>
typedef void (^BlockValue)(NSString *valueString);

@interface SecondViewController : UIViewController

@property (nonatomic,copy) BlockValue blockValue;
- (void)getValue:(BlockValue)aBlock;

@end

SecondViewController.m 中:

    #import "SecondViewController.h"
    @interface SecondViewController ()
    @property (nonatomic,strong) UITextField *textField;
    @end
    
    @implementation SecondViewController

 - (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UITextField *textField = [[UITextField alloc] init];
    textField.frame = CGRectMake(50, 200, 200, 50);
    textField.placeholder = @"请输入需要传值的数据";
    [self.view addSubview:textField];
    self.textField = textField;
}

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

 - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        if (self.blockValue) {
            self.blockValue(_textField.text);
        }
}
 @end
    

2.在A界面定义SecondViewController:

#import "OneViewController.h"
#import "SecondViewController.h" 

@interface OneViewController ()
@property (nonatomic,strong) UILabel *label;     
@end

@implementation OneViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UILabel *label = [[UILabel alloc] init];
    label.text = @"等待传值";
    label.frame = CGRectMake(50, 200, 200, 50);
    [self.view addSubview:label];
    self.label = label;

     UIButton *button = [UIButton         buttonWithType:UIButtonTypeCustom];
     button.frame =CGRectMake(50, 500, 80, 50);
     [button setTitle:@"下一页" forState:UIControlStateNormal];
     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
     [button addTarget:self action:@selector(nextStep:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:button];
}

- (void)nextStep:(UIButton *)button {
    SecondViewController *secondVc = [[SecondViewController alloc] init];
    secondVc.blockValue = ^(NSString *valueString) {
        self.label.text = valueString;
    };
    [self.navigationController pushViewController:secondVc animated:YES]; 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
    原文作者:差一点很帅
    原文地址: https://segmentfault.com/a/1190000006260439
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞