iOS深入学习(再谈block)

来源:互联网 发布:架空线路设计软件 编辑:程序博客网 时间:2024/05/19 18:42
之前写过一篇博客,把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 *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;//Xcode中复制过来的我特别把delegate参数用红色注明,所以是不是有了一点怎样去定义block作为参数的灵感,下面我就写完整的代码,分享我自己的经验吧,首先,还是要说一个场景,自定义的cell上面有一个Button,点击Button,调用在ViewController中的-(void)OperateLog:方法,不多说了,上代码,ImageTableViewCell.h filetypedef 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;@endImageTableViewCell.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");}这篇博客和之前的博客,请有心人一定要仔细看明白,如果有什么疑问我一定详细解答,因为我觉得Block是我自己最花了时间研究的,如果其他的人能够通过我的博客而涨了姿势的话,我肯定是很开心的。还要提醒有心人,看懂了的人自然明白这篇博客的精髓和犀利。

0 0
原创粉丝点击