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
- 运行测试
- 点击跳转按钮到第二个视图,并在textField中输入内容
- 点击返回按钮返回到第一个视图,label的内容改变,传值成功