ios block1
来源:互联网 发布:公司申请淘宝企业店铺 编辑:程序博客网 时间:2024/05/16 19:49
之前写过一篇博客,把Block跟delegate类比,说明了使用block,可以通过更少的代码实现代理的功能。那篇博客将block定义为类的property。
过了这么长时间,对于block的内容有了很多的遗忘,果然block的语法比较操蛋,容易遗忘,还是看看http://fuckingblocksyntax.com/,复习一下操蛋的block语法,我翻译如下,
(1)block作为本地变量(local variable)
returnType (^blockName)(parameterTypes) = ^returnType(parameters){...};
(2)block作为类的成员属性(@property)
@property (nonatomic, copy) returnType (^blockName)(parameters);
这时候可以类比delegate,实现代理功能。
(3)block作为函数参数(method parameter)
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
调用包括block参数的函数,
[someObject somethodThatTakesABlock:^returnType(parameters){...}];
(4)使用typedef定义block类型
typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters){...};
上面的内容翻译自fuckblocksyntax,大家忘了block语法的时候可以反复的看看。
Block的使用举例,
(1)作为本地变量
int (^Mutiply)(int,int) = ^(int num1,int num2){
return num1*num2;
};
block可以访问局部变量,但是不能修改,否则会编译报错,
int mutiplier = 7;
int (^myBlock)(int) = ^(int num){
mutiplier++;//编译报错
return num*mutiplier;
};
如果要在block内部修改局部变量,则需要使用__block来修饰该局部变量,如下,
__block int mutiplier = 7;
int (^myBlock)(int) = ^(int num){
mutiplier++;//编译不会报错
return num*mutiplier;
};
(2)作为函数参数使用
作为函数的参数,block某种意义上替代了回调函数或者delegate,当函数调用的时候,假设某个事件发生,这时候block里面的内容就会运行。这样有利于代码的整合和阅读,不需要到处去实现委托方法了。
Block作为函数参数,到底该怎么写代码呢?今天(2014.05.09)晚上突然想明白,只要坚定的把Block当做delegate,一切就很简单了,我们先看看delegate作为参数,是怎么写的吧,如下代码,
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"点击Cell" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
这里是初始化一个alert并且让alert弹出,让我们看看初始化的-(void)initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:是怎么定义的吧,如下代码,
- (id)initWithTitle:(NSString *)title message:(NSString *)messagedelegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;//Xcode中复制过来的
我特别把delegate参数用红色注明,所以是不是有了一点怎样去定义block作为参数的灵感,下面我就写完整的代码,分享我自己的经验吧,
首先,还是要说一个场景,自定义的cell上面有一个Button,点击Button,调用在ViewController中的-(void)OperateLog:方法,
不多说了,上代码,
ImageTableViewCell.h file
typedef void (^BtnBlock)();//定义一个block
@interface ImageTableViewCell:UITableViewCell
{
BtnBlock _btnBlock;//定义一个block成员变量
}
//下面是两个xib拖动的控件
@property (nonatomic, strong) IBOutlet UIButton *btn;
@property (nonatomic, strong) IBOutlet UIIageView *imgView;
- (void)configureCell:(UITableViewCellStyle)style Block:(BtnBlock)block reuseIdentifier:(NSString *)reuseIdentifier;
//点击Button触发的方法
- (IBAction)btnClicked:(id)sender;
@end
ImageTableViewCell.m file
@implementation ImageTableViewCell
//...省略无关代码
- (void)configureCell:(UITableViewCellStyle)style Block:(BtnBlock)block reuseIdentifier:(NSString *)reuseIdentifier
{
//...省略不相干代码
_btnBlock = block;
}
- (IBAction)btnClicked:(id)sender
{
if(_btnBlock)
{
_btnBlock();
}
}
ViewController.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleIdentify = @"UITableViewCellIdentify";
ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
if (!cell) {
//将Custom.xib中的所有对象载入
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ImageTableViewCell" owner:self options:nil];
//第一个对象就是CustomCell了
cell = [nib objectAtIndex:0];
[cell configureCell:UITableViewCellStyleDefault Block:^{
[self OperateLog];
} reuseIdentifier:simpleIdentify];
}
return cell;
}
//button点击,最终传递到OperateLog方法,相当于代理方法吧
- (void)OperateLog
{
NSLog(@"点击了cell上面的Button");
}
- ios block1
- block1
- Object Builder Application Block1
- iOS
- iOS
- IOS
- iOS
- iOS
- IOS
- ios
- iOS
- iOS
- IOS
- iOS
- ios
- ios ~~~~~
- ios
- IOS
- android ble蓝牙开发总结(附带自己项目中提取整理的demo地址)
- c++ 函数模板
- ios系统和某些移动端background-attachment:fixed不兼容性
- iOS 获取当前时间戳和时间
- 欢迎使用CSDN-markdown编辑器
- ios block1
- ios经验收藏 使用objection来模块化开发iOS项目
- iOS里 UITextField以特定字符串组成
- 不能用ajax请求下载文件
- 123
- hdu1312 Red and Black(DFS深搜,入门题目)
- 用 VMware 在 PC 上使用 Android
- Xcode引用C++头文件出现 Unknown type name 'namespace' 错误的解决方法
- 解决Android 中Edittext在执行setError时有时不显示文字