block语法基础与应用
2015-07-31 22:00
246 查看
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);4.typedef 的使用使用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));5.block做方法的参数
[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上ViewController.h
#import <UIKit/UIKit.h> #import "OpenView.h" @interface ViewController : UIViewController { UILabel *_label; OpenView *view; } @endViewController.m
#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. } @endOpenView.h
#import <UIKit/UIKit.h> typedef void (^Type) (void); @interface OpenView : UIView { UIButton *_btn; Type _block; } @property (nonatomic, retain) UITextView *textView; - (void)setBlo:(Type)block; @endOpenView.m
#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
相关文章推荐
- OC NSString的几种常用方法 更全面
- 黑马程序员------OC ARC管理和Category
- hdu 4026 2011上海赛区网络赛F TSP ****
- hdu 5296 Annoying problem(LCA)
- 利用for循环和条件运算符输出一个数组中最大和最小值并分别输出下标
- java.容器
- 【iOS】FMDB/SQLCipher数据库加解密,迁移
- Entityframework~Fluent Api配置数据模型与数据库结构关系
- word2vector使用
- HDU 4311&4312 Meeting point-1&2 (曼哈顿距离&&切比雪夫距离)
- 动态代理
- HDU_5327_Olympiad
- TOJ 2470 -- Robot in Maze
- JSP学习笔记(三)---JSTL标签核心库
- div+css
- 暑假-动态规划 III-(H - 最少拦截系统)
- 【递推+高精度】POJ2506-Tiling
- 背包问题详解
- 后端撸前端系列之 - 图片水平垂直居中
- 敏捷开发实战(三)--每日晨会,是否只是摆设?