【iOS】—— Block传值

Block简介

将函数及其执行上下文封装起来的对象,block的调用实际就是函数的调用。

Block传值的步骤

  • 步骤1:block 的声明 (视图二 .h 中)

返回值类型(^block 的名字)(参数列表);

//自定义类型Block
typedef void(^returnLabelValue)(NSString *label);

//Block声明
@property (nonatomic, strong) returnLabelValue valueLabel;
  • 步骤二:block 的调用 (视图二 .m 中)

block的名字();

    //Block传值
    _valueLabel(_textField.text);
  • 步骤三:block 实现(视图一新视图的创建中)

block的名字 = ^(参数列表) { 使用调用的参数列表 };

change.valueLabel = ^(NSString *text) {
        self.label.text = text;
    };

Block的小测试

  • 视图二的 .h 中
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

//自定义类型Block
typedef void(^returnLabelValue)(NSString *label);

@interface NewViewController : UIViewController<UITextFieldDelegate>

//用于返回第一个界面
@property (nonatomic, strong) UIButton *backButton;
//返回输入的text
@property (nonatomic, strong) UITextField *textField;
//声明一个returnLabelValue属性,这个Block是获取传值的界面传进来的
@property (nonatomic, strong) returnLabelValue valueLabel;

@end

NS_ASSUME_NONNULL_END
  • 视图二的 .m 中
#import "NewViewController.h"

@interface NewViewController ()

@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [_backButton setTitle:@"返回" forState:UIControlStateNormal];
    _backButton.frame = CGRectMake(200, 200, 60, 40);
    [self.view addSubview:_backButton];
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 300, 250, 50)];
    _textField.delegate = self;
    _textField.keyboardType = UIKeyboardTypeDefault;
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textField];
}

- (void)back:(UIButton*)button {
    //Block传值
    //这种方式并不会引起循环引用,而如果在这里将闭包体写进来并引用了self会引起循环引用
    if (self.valueLabel) {
        //将自己的值传出去,完成传值
        _valueLabel(_textField.text);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
  • 视图一的 .h 中
#import <UIKit/UIKit.h>
#import "NewViewController.h"

@interface ViewController : UIViewController

//用于跳转到第二个界面
@property (nonatomic, strong) UIButton *changeButton;
//显示传过来的数据
@property (nonatomic, strong) UILabel *label;

@end
  • 视图一的 .m 中
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _changeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_changeButton setTitle:@"跳转" forState:UIControlStateNormal];
    [_changeButton addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    _changeButton.frame = CGRectMake(200, 300, 60, 40);
    [self.view addSubview:_changeButton];
    
    _label = [[UILabel alloc] init];
    _label.text = @"内容";
    _label.frame = CGRectMake(100, 100, 100, 50);
    [self.view addSubview:_label];
}

- (void)change:(UIButton*)button {
    NewViewController *change = [[NewViewController alloc] init];
    change.modalPresentationStyle = UIModalPresentationFullScreen;
    
    //赋值Block,并将捕获的值赋值给UILabel
    change.valueLabel = ^(NSString *text) {
        self.label.text = text;
    };
    [self presentViewController:change animated:YES completion:nil];
}

@end
  • 运行测试

231

  • 点击跳转按钮到第二个视图,并在textField中输入内容

在这里插入图片12

  • 点击返回按钮返回到第一个视图,label的内容改变,传值成功

453

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值