1.block变量的声明定义
void (^block) (void);
此处void是返回值类型,block是块变量名,(void)是形参
[不难发现block的声明与函数指针的声明类似:void (*point) (void)]
这只是一种block类型,根据不同的返回值类型或者形参可以定义各种 block变量:
例如:(1) int (^block1) (int,float)
(2) NSString * (^block2) (NSInteger , NSString *)
etc..
2.创建block
block = ^(void) {
NSLog(@"hello");
};
同样还有:
block1 = ^int(int a, float b){
return a+b;
};
block2 = ^NSString *(NSInteger a,NSString *b){
NSLog(@"a = %ld,b = %@",a,b);
};
etc..
3.调用block
block();
void (^myBlock) (int);
myBlock = ^(int a){
NSLog(@"%d",a);
};
myBlock(3);
int (^block1) (int);
block1 = ^(int b){
return b;
};
int ss = block1(90);
NSLog(@"%d",ss);
使用typedef可以简洁block代码,如:
typedef void (^Type1) (void);
typedef NSString* (^Type2) (NSString *);
typedef int (^Type3) (int,int);
Type1 block2 = ^(void) {
NSLog(@"TYPE block2");
};
block2();
Type2 block3 = ^(NSString *str) {
return [NSString stringWithFormat:@"hahahaha %@",str];
};
NSString *str = block3(@"hyr");
NSLog(@"%@",str);
Type3 block4 = ^(int a,int b) {
return a*b;
};
NSLog(@"%d",block4(2,6));
[self testBlock1:^NSString*(NSString *str){
return [NSString stringWithFormat:@"hahahaha %@",str];
}];
6.应用
用block方式完成弹出窗口中内容的回传:(1)主视图中有个label和button0
(2)点击button后弹出一个视图A
(3)视图A上有个输入框和button1
(4)在输入框中输入文字后,点击button1后视图A消失,并将输入框中输入的文字显示在label上
#import
#import "OpenView.h"
@interface ViewController : UIViewController
{
UILabel *_label;
OpenView *view;
}
@end
#import "ViewController.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
typedef void (^Type) (void);
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_label = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, kWidth-40, 400)];
_label.numberOfLines = 0;
_label.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_label];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(70, 500, kWidth-140, 50)];
button.backgroundColor = [UIColor greenColor];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
view = [[OpenView alloc] initWithFrame:CGRectMake(50, 100,kWidth-100 , 300)];
view.backgroundColor = [UIColor blackColor];
view.hidden = YES;
[self.view addSubview:view];
Type block = ^{
_label.text = view.textView.text;
};
[view setBlo:block];
// _label.text = [view ];
}
- (void)buttonAction {
view.hidden = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import
typedef void (^Type) (void);
@interface OpenView : UIView
{
UIButton *_btn;
Type _block;
}
@property (nonatomic, retain) UITextView *textView;
- (void)setBlo:(Type)block;
@end
#import "OpenView.h"
@implementation OpenView
- (instancetype)initWithFrame:(CGRect)frame {
self= [super initWithFrame:frame];
if (self) {
_textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width-10, 200)];
_textView.backgroundColor = [UIColor yellowColor];
[self addSubview:_textView];
_btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 210+20, self.bounds.size.width-40, 40)];
_btn.backgroundColor = [UIColor blueColor];
[_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_btn];
}
return self;
}
- (void)btnAction:(UIButton *)btn {
_block();
self.hidden = YES;
}
- (void)setBlo:(Type)block {
_block = [block copy];
}
@end