赞
踩
将函数及其执行上下文封装起来的对象,block的调用实际就是函数的调用。
返回值类型(^block 的名字)(参数列表);
//自定义类型Block
typedef void(^returnLabelValue)(NSString *label);
//Block声明
@property (nonatomic, strong) returnLabelValue valueLabel;
block的名字();
//Block传值
_valueLabel(_textField.text);
block的名字 = ^(参数列表) { 使用调用的参数列表 };
change.valueLabel = ^(NSString *text) {
self.label.text = text;
};
#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
#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
#import <UIKit/UIKit.h>
#import "NewViewController.h"
@interface ViewController : UIViewController
//用于跳转到第二个界面
@property (nonatomic, strong) UIButton *changeButton;
//显示传过来的数据
@property (nonatomic, strong) UILabel *label;
@end
#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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。