Block简介:
block是存储的数据是一个函数体。使用block是,你可以像调用其他标准函数一样,传入参数数,并得到返回值。
脱字符(^)是块的语法标记。按照我们熟悉的参数语法规约所定义的返回值以及块的主体(也就是可以执行的代码)。
block的定义:
//1 定义一个block变量
/*
* int 返回值类型
* myBlock block变量的名称
* int a,NSString *s 参数
*/
int (^myBlock)(int a,NSString *s);
// int (^myBlock)(int ,NSString *);
// int (^)(int a,NSString *s)[类型] myblock[变量名]
//2 创建一个block
myBlock =^(int a,NSString *s){
//block要调用的代码块
NSLog(@"myBlock被调用了");
NSLog(@"a =%d s = %@",a,s);
return a; //和定义的block返回值类型对应.
};
block最主要的用途就是作为方法的参数在使用的,我们来看看如何使用block在viewcontroller与view上来传递值的吧。
#import <UIKit/UIKit.h>
//创建block的类型与名称。注意ChangeBlock是别名,代替了 void(^)(NSString *text),ios中规定这么写
typedef void(^ChangeBlock)(NSString *text);
@interface BlockChangeValue : UIView{
UIButton *button;
}
- (void)changeTextValue:(ChangeBlock)block;
@property(nonatomic,strong)ChangeBlock block;
//
// BlockChangeValue.m
// BlockChangeValueandDelegate
//
// Created by mac on 16/8/2.
// Copyright © 2016年 mac. All rights reserved.
//
#import "BlockChangeValue.h"
@implementation BlockChangeValue
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self != nil) {
self.backgroundColor = [UIColor orangeColor];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 200, 80)];
textField.placeholder = @"请输入";
textField.layer.cornerRadius = 10;
textField.textColor = [UIColor blackColor];
textField.tag = 1000;
textField.backgroundColor = [UIColor whiteColor];
[self addSubview:textField];
button = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - 30)/2, 120, 50, 30)];
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"确定" forState:UIControlStateNormal];
[self addSubview:button];
}
return self;
}
//使用block方法
- (void)changeTextValue:(ChangeBlock)block {
self.block = block;
[button addTarget:self action:@selector(buttnAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttnAction:(UIButton *)button{
UITextField *textFiled = [self viewWithTag:1000];
//将你需要的值进行传递
self.block(textFiled.text);
[self removeFromSuperview];
}
@end
接收方:
//
// ViewController.m
// BlockChangeValueandDelegate
//
// Created by mac on 16/8/2.
// Copyright © 2016年 mac. All rights reserved.
//
#import "ViewController.h"
#import "BlockChangeValue.h"
@interface ViewController (){
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 300, 200)];
label.backgroundColor = [UIColor grayColor];
label.textColor = [UIColor whiteColor];
label.text = @"传值";
label.textAlignment = NSTextAlignmentCenter;
label.tag = 1000;
UIButton *blockBtn = [[UIButton alloc] initWithFrame:CGRectMake(220, 250, 80, 30)];
[blockBtn setTitle:@"block打开" forState:UIControlStateNormal];
[blockBtn addTarget:self action:@selector(blockBtnAction:) forControlEvents:UIControlEventTouchUpInside];
blockBtn.backgroundColor = [UIColor blackColor];
[self.view addSubview:blockBtn];
//背景颜色
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];
}
- (void)blockBtnAction:(UIButton *)button{
BlockChangeValue *blockVC = [[BlockChangeValue alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
blockVC.center = self.view.center;
//使用block方法进行值得改变
[blockVC changeTextValue:^(NSString *text) {
UILabel *label = [self.view viewWithTag:1000];
label.text = text;
}];
[self.view addSubview:blockVC];
}
@end