[iOS]-Block传值

Block简介

block 就是带有自动变量(就是局部变量)值的匿名函数。顾名思义就是带有自动变量(也就是局部变量)值的不带名称的函数。

Block传值

Block传值是类似于协议传值的反向传值,即都是从B界面传回A界面

Block传值的过程

  1. 先在B界面的.h文件中定义代码块和block属性
  2. 在A界面的.m文件中设置事件,从A界面跳转到B界面,并编写B界面的block将相应值传给A界面属性的代码
  3. 在B界面.m文件的触发传值的事件中将需要传的值做为block代码块的参数,并返回到A界面即可看到完成了block传值

Block传值的例子:

例如我们在A界面创建一个label和button,label用于在屏幕上显示接收到的值,button用于点击跳转界面和触发传值过程,然后我们在B界面创建一个TextField和button,TextField用于输入所要传给A界面的字符串,button用于点击跳传界面和传值

在B界面中的.h文件中:

#import <UIKit/UIKit.h>

//定义一个代码块,最后面的括号中为block传的参数,可以传多个参数
typedef void (^Testblock) (NSString *string);

@interface TestViewController : UIViewController

@property (nonatomic, strong) UITextField *textField;
//定义一个block属性
@property (nonatomic, copy) Testblock block;

@end

在B界面的.m文件中:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //用于输入所要传给A界面的字符串的TextField
    _textField = [[UITextField alloc] init];
    _textField.frame = CGRectMake(114, 300, 200, 50);
    _textField.layer.borderColor = [UIColor blackColor].CGColor;
    _textField.layer.borderWidth = 1;
    _textField.layer.cornerRadius = 8.0;
    _textField.layer.masksToBounds = YES;
    [self.view addSubview:_textField];
    
    //用于点击跳传界面和传值的button
    UIButton *buttonFirst = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonFirst setTitle:@"返回" forState:UIControlStateNormal];
    [buttonFirst addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    buttonFirst.frame = CGRectMake(174, 400, 80, 40);
    buttonFirst.backgroundColor = [UIColor yellowColor];
    buttonFirst.layer.cornerRadius = 8.0;
    buttonFirst.layer.masksToBounds = YES;
    [self.view addSubview:buttonFirst];

}

- (void) pressButton {
    //将需要传出的值作为block代码块的参数
    _block(self.textField.text);
    //退出当前视图
    [self dismissViewControllerAnimated:YES completion:nil];
}
    
@end

A界面的.h文件中:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
//用于在屏幕上显示所接收值的label
@property (nonatomic, strong) UILabel *labelFirst;

@end

A界面的.m文件中:

#import "ViewController.h"
#import "TestViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    //用于点击跳转界面和触发传值过程的button
    UIButton *buttonFirst = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonFirst setTitle:@"跳转" forState:UIControlStateNormal];
    [buttonFirst addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    buttonFirst.frame = CGRectMake(174, 400, 80, 40);
    buttonFirst.backgroundColor = [UIColor yellowColor];
    buttonFirst.layer.cornerRadius = 8.0;
    buttonFirst.layer.masksToBounds = YES;
    [self.view addSubview:buttonFirst];
    
    //用于在屏幕上显示所接收值的label
    _labelFirst = [[UILabel alloc] init];
    _labelFirst.frame = CGRectMake(174, 300, 80, 30);
    _labelFirst.text = @"Block传值";
    [self.view addSubview:_labelFirst];
}

//点击按钮后跳转到第二个视图界面
- (void) pressButton {
    TestViewController *testViewController = [[TestViewController alloc] init];
    testViewController.modalPresentationStyle = UIModalPresentationFullScreen;
    //推出B界面
    [self presentViewController:testViewController animated:YES completion:nil];
    //在B的block中设置传值过程
    [testViewController setBlock:^(NSString * _Nonnull string) {
            self.labelFirst.text = string;
        }];
}

@end

运行结果:
初始的A界面:
请添加图片描述
点击跳转按钮后跳转到B界面,然后在B界面的TextField中输入123456:
请添加图片描述
点击返回按钮后跳转到A界面:
请添加图片描述
可以发现A界面的label的内容已经变为了123456,此时全部的传值过程就完成了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值