OC中的block使用初探

来源:互联网 发布:五子棋定式软件 编辑:程序博客网 时间:2024/06/05 22:51

iOS 4.0 引入了 block 这个概念,swift 语言中的闭包其实也和block 类似。block和其他变量类似,只是他是指向的函数实现地址。

直接进入主题,以一个简单的block问题为切入点,写一个block,将两个字符串合并成为一个字符串,很简单大笑

申明:

NSString * (^myBlock)(NSString *, NSString *);
定义已经成功:myBlock为block名称,这个名称外面的括号是必须的哦,这和经常绕晕大家的函数的指针,指针的函数一样一样的,涉及到运算顺序,不多解释。   

 第一个NSString * 表示这个block的返回值,如果没有返回值就是void,但是必须要填写哦。

里面有两个参数,表示需要传入的参数

定义:

    myBlock = ^(NSString *str1, NSString *str2) {        return [NSString stringWithFormat:@"%@%@",str1,str2];    };

使用

    //block的使用    NSString *result = myBlock(@"str1", @"str2");    NSLog(@"result = %@",result); //result = str1str2

到此为止,如果是一道面试题,面试官应该会给你满分啦。


声明和定义可以放在一起,所以也可以写成

    NSString * (^myBlock)(NSString *str1, NSString *str2) = ^(NSString *str1, NSString *str2) {        return [NSString stringWithFormat:@"%@%@",str1,str2];    };

我们再以一个小小的例子去说明:



有一个ViewController, 有一个button,初始化会显示第一行,但是点击这个button,弹出一个uiview(定义为DDOptionView), 要从“第一行”到“第六行”中间选择一行,controller中的button要显示这行文字。

以前常见的做法是用代理模式,实现两个对象之间的相互协调合作。block出现后,可以实现类似的功能,并且简洁了(例如不用再去写协议类等)。直接上代码:

在 DDOptionView的h文件里面:

#import <UIKit/UIKit.h>typedef void (^DDOptionViewTableviewBlock)(NSInteger row); //定义block@interface DDOptionView : UIView/** *  初始化方法 * *  @param superview     本UIView要添加的UIView *  @param sIndex        已经选择的行 *  @param optionArray   本UIView的数据数组 *  @param selectedBlock 点击每行后调用的block * *  @return DDOptionView */- (id)initWithSuperView:(UIView *)superview          selectedIndex:(NSInteger)sIndex                options:(NSArray *)optionArray tableviewSelectedBlock:(DDOptionViewTableviewBlock)selectedBlock;@end
在 DDOptionView的m文件里面:定义一个属性
@interface DDOptionView () <UITableViewDataSource, UITableViewDelegate>
//...@property (nonatomic, copy) DDOptionViewTableviewBlock tableCellSelectedBlock; 
//....@end
初始化的时候会和普通的变量一样给属性赋值
- (id)initWithSuperView:(UIView *)superview          selectedIndex:(NSInteger)sIndex                options:(NSArray *)optionArray tableviewSelectedBlock:(DDOptionViewTableviewBlock)selectedBlock{    if (self = [super init]) {        self.tableCellSelectedBlock = selectedBlock;        //...    }    return self;}

点击后执行block:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    [tableView deselectRowAtIndexPath:indexPath animated:YES];    //...    self.tableCellSelectedBlock(indexPath.row);    [self removeFromSuperview];}

给viewcontroller 的showBtn 设置点击方法,添加OptionView并且给点击的block 赋值:

    NSArray *optionArr = @[@"第一行",@"第二行",@"第三行",@"第四行",@"第五行",@"第六行"];    DDOptionView *option = [[DDOptionView alloc] initWithSuperView:self.view                                                   selectedIndex:selectedIndex                                                         options:optionArr                                          tableviewSelectedBlock:^(NSInteger row) {                                              [showBtn setTitle:optionArr[row-1] forState:UIControlStateNormal];                                              selectedIndex = row;                                          }];    [self.view addSubview:option];


是不是比得delegate 更灵活,代码更简洁。大笑

附加代码

0 0
原创粉丝点击