block和代理的学习

(一)

Block很简单,就像delegate的简化版

摘要  block的语法让人很蛋疼,但是如果好好理一下思路,发现也没那么难。

代理设计模式对于iOS开发的人来说肯定很熟悉了,代理delegate就是委托另一个对象来帮忙完成一件事情,为什么要委托别人来做呢,这其实是MVC设计模式中的模块分工问题,例如View对象它只负责显示界面,而不需要进行数据的管理,数据的管理和逻辑是Controller的责任,所以此时View就应该将这个功能委托给Controller去实现,当然你作为码农强行让View处理数据逻辑的任务,也不是不行,只是这就违背了MVC设计模式,项目小还好,随着功能的扩展,我们就会发现越写越难写;还有一种情况,就是这件事情做不到,只能委托给其他对象来做了,下面的例子中我会说明这种情况。

下面的代码我想实现一个简单的功能,场景描述如下:TableView上面有多个CustomTableViewCell,cell上面显示的是文字信息和一个详情Button,点击button以后push到一个新的页面。为什么说这个场景用到了代理delegate?因为button是在自定义的CustomTableViewCell上面,而cell没有能力实现push的功能,因为push到新页面的代码是这样的,

[self.navigationController pushViewController...];

所以这时候CustomTableViewCell就要委托它所在的Controller去做这件事情了。

按照我的编码习惯,我喜欢把委托的协议写在提出委托申请的类的头文件里面,现在的场景中是CustomTableViewCell提出了委托申请,下面是简单的代码,

@protocol CustomCellDelegate <NSObject>

- (void)pushToNewPage;

@end


@interface CustomTableViewCell : UITableViewCell

@property(nonatomicassign) id<CustomCellDelegatedelegate;

@property (nonatomicstrongUILabel *text1Label;

@property(nonatomic,strong) UIButton *detailBtn;

@end

上面的代码在CustomTableViewCell.h中定义了一个协议CustomCellDelegate,它有一个需要实现的pushToNewPage方法,然后还要写一个属性修饰符为assign、名为delegate的property,之所以使用assign是因为这涉及到内存管理的东西,以后的博客中我会专门说明原因。

接下来在CustomTableViewCell.m中编写Button点击代码,

[self.detailBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

对应的btnClicked方法如下,

- (void)btnClicked:(UIButton *)btn

{

    if (self.delegate && [self.delegaterespondsToSelector:@selector(pushToNewPage)]) {

        [self.delegate pushToNewPage];

    }

}

上面代码中的判断条件最好是写上,因为这是判断self.delegate是否为空,以及实现CustomCellDelegate协议的Controller是否也实现了其中的pushToNewPage方法。

接下来就是受到委托申请的类,这里是对应CustomTableViewCell所在的ViewController,它首先要实现CustomCellDelegate协议,然后要实现其中的pushToNewPage方法,还有一点不能忘记的就是要设置CustomTableViewCell对象cell的delegate等于self,很多情况下可能忘了写cell.delegate = self;导致遇到问题不知云里雾里。下面的关键代码都是在ViewController.m中,

首先是服从CumtomCellDelegate协议,这个大家肯定都知道,就像很多系统的协议,例如UIAlertViewDelegate、UITextFieldDelegate、UITableViewDelegate、UITableViewDatasource一样。

@interface ViewController ()<CustomCellDelegate>

@property (nonatomicstrong) NSArray *textArray;


@end

然后是实现CustomCellDelegate协议中的pushToNewPage方法,

- (void)pushToNewPage

{

    DetailViewController*detailVC = [[DetailViewController alloc] init];

    [self.navigationController pushViewController:detailVC animated:YES];

}

还有一个步骤最容易被忘记,就是设置CumtomTableViewCell对象cell的delegate,如下代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *simpleIdentify = @"CustomCellIdentify";

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];

    if (cell == nil) {

        cell = [[CustomTableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify];

    }

    //下面代码很关键

    cell.delegate = self;

    cell.text1Label.text = [self.textArray objectAtIndex:indexPath.row];

    return cell;

}

通过cell.delegate = self;确保了CustomTableViewCell.m的判断语句if(self.delegate && ...){}中得self.delegate不为空,此时的self.delegate其实就是ViewController,cell对象委托了ViewController实现pushToNewPage方法。这个简单的场景描述了使用代理的一种情况,就是CustomTableViewCell没有能力实现pushViewController的功能,所以委托ViewController来实现。

代码在github可以下载。

有什么错误,还请大家指正。

----------------------------------------------下面是block的内容----------------------------------------------------

Block是一个C语言的特性,就像群里有人说的,它就是C语言的函数指针,在使用中最多的就是进行函数回调或者事件传递,比如发送数据到服务器,等待服务器反馈是成功还是失败,此时block就派上用场了,这个功能的实现也可用使用代理,这么说的话,感觉block是不是有点像代理了呢?

我之前接触block,都是使用它作为函数参数,当时感觉不是很理解。现在在项目中,很多时候block作为property,这样更加简单直接,想想,其实property不就是定义的合成存储的变量嘛,而block作为函数参数也是定义的变量,所以作为函数参数或者作为property本质没有区别。

看一看别人总结的block的语法吧,http://fuckingblocksyntax.com,这个链接亮了,fucking block syntax,操蛋的block语法啊。block有如下几种使用情况,

1、作为一个本地变量(local variable)

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

2、作为@property

@property (nonatomic, copy) returnType (^blockName)(parameterTypes);

3、作为方法的参数(method parameter)

- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

4、作为方法参数的时候被调用

[someObject someMethodThatTakesABlock: ^returnType (parameters) {...}];

5、使用typedef来定义block,可以事半功倍

typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};

上面我也只是复制粘贴了一下,接下来还是实现点击CustomTableViewCell上面的Button实现页面跳转的功能,我之前不止一次的类比block就像delegate,这边我也是思维惯性,下面的内容我就当block为代理,一些用词描述还是跟delegate差不多。首先,在提出委托申请的CustomTableViewCell中定义block的property,

@interface CustomTableViewCell : UITableViewCell

@property (nonatomicstrongUILabel *text1Label;

@property (nonatomicstrongUIButton *detailBtn;


//下面的定义,请看官们对比一下

/*delegate的定义 我没有删除,因为大家可以类比了看下*/

@property (nonatomicassignid<CustomCellDelegate> delegate;

/*这里定义了ButtonBlock*/

@property (nonatomiccopyvoid (^ButtonBlock)();

@end

这里用copy属性来修饰ButtonBlock property,这个原因,我会在以后的博客中作专门的解释。

接下来在CustomTableViewCell中给它上面的detailBtn绑定点击方法,

[self.detailBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

然后是btnClicked方法的细节,我把delegate的内容也没有删除,就是给各位比较一下block和delegate的功能和语法的相似性,

- (void)btnClicked:(UIButton *)btn

{

    //这是之前的delegate

    if (self.delegate && [self.delegate respondsToSelector:@selector(pushToNewPage)]) {

        [self.delegate pushToNewPage];

    }

    

    //这是现在我们要说的block    

    if (ButtonBlock) {

        ButtonBlock();

    }

}

下面是一个关键性的地方,在ViewController2中设置其CustomTableViewCell的cell对象的ButtonBlock,也就是给它赋值,此处我还是保留了cell.delegate = self;代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *blockIdentify = @"BlockIdentify";

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:blockIdentify];

    if (cell == nil) {

        cell = [[CustomTableViewCell allocinitWithStyle:UITableViewCellStyleDefault reuseIdentifier:blockIdentify];

    }

    cell.text1Label.text = [self.textArray objectAtIndex:indexPath.row];

    //delegate的不可缺少的代码,这里放在这儿只是为了给各位类比一下

    cell.delegate = self;

    

    //ButtonBlock不可缺少的代码

    cell.ButtonBlock = ^{

        [self pushToNewPage2];

    };

    return cell;

}

之所以cell.ButtonBlock = ^{};赋值,是因为我们我们是这样定义ButtonBlock的,void (^ButtonBLock)(),表示无返回值无参数。

然后编写pushToNewPage2方法,

- (void)pushToNewPage2

{

    DetailViewController *detailVC = [[DetailViewController allocinit];

    [self.navigationController pushViewController:detailVC animated:YES];

}

你们看这个方法是不是与CustomCellDelegate协议中的pushToNewPage方法类似。然后在回过头来类比一样,是不是block就是精简版的delegate,因为delegate设计模式要写协议CustomCellDelegate,还有容易遗漏cell.delegate = self;但是block使用的时候就简单多了。

最新的代码我已经更新到github,非常简单,有疑问或者有质疑的朋友可以下载看看。

(二)

iOS深入学习(再谈block)

摘要  block内容有点遗忘,还是要温故知新,不断学习

之前写过一篇博客,把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 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深入学习(Block全面分析)

摘要  学习Block从迷惑,到略懂,从理解到顿悟,在此与大家分享。

本文翻译自苹果的文档,有删减,也有添加自己的理解部分。

如果有Block语法不懂的,可以参考fuckingblocksyntax,里面对于Block

为了方便对比,下面的代码我假设是写在ViewController子类中的

1、第一部分

定义和使用Block,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- ( void )viewDidLoad
{
     [super viewDidLoad];
     //(1)定义无参无返回值的Block
     void  (^printBlock)() = ^(){
         printf ( "no number" );
     };
     printBlock();
     
 
     printBlock(9);
     
     int  mutiplier = 7;
     //(3)定义名为myBlock的代码块,返回值类型为int
     int  (^myBlock)( int ) = ^( int  num){
         return  num*mutiplier;
     }
     //使用定义的myBlock
     int  newMutiplier = myBlock(3);
     printf ( "newMutiplier is %d" ,myBlock(3));
}
//定义在-viewDidLoad方法外部
//(2)定义一个有参数,没有返回值的Block
void  (^printNumBlock)( int ) = ^( int  num){
     printf ( "int number is %d" ,num);
};

定义Block变量,就相当于定义了一个函数。但是区别也很明显,因为函数肯定是在-viewDidLoad方法外面定义,而Block变量定义在了viewDidLoad方法内部。当然,我们也可以把Block定义在-viewDidLoad方法外部,例如上面的代码块printNumBlock的定义,就在-viewDidLoad外面。

再来看看上面代码运行的顺序问题,以第(3)个myBlock距离来说,在定义的地方,并不会执行Block{}内部的代码,而在myBlock(3)调用之后才会执行其中的代码,这跟函数的理解其实差不多,就是只要在调用Block(函数)的时候才会执行Block体内(函数体内)的代码。所以上面的简单代码示例,我可以作出如下的结论,

(1)在类中,定义一个Block变量,就像定义一个函数;

(2)Block可以定义在方法内部,也可以定义在方法外部;

(3)只有调用Block时候,才会执行其{}体内的代码;

(PS:关于第(2)条,定义在方法外部的Block,其实就是文件级别的全局变量)

那么在类中定义一个Block,特别是在-viewDidLoad方法体内定义一个Block到底有什么意义呢?我表示这时候只把它当做私有函数就可以了。我之前说过,Block其实就相当于代理,那么这时候我该怎样将其与代理类比以了解呢。这时候我可以这样说:本类中的Block就相当于类自己服从某个协议,然后让自己代理自己去做某个事情。很拗口吧?看看下面的代码,

?
1
2
3
4
5
6
7
8
9
10
//定义一个协议
@protocol ViewControllerDelegate<NSObject>
- ( void )selfDelegateMethod;
@end
 
//本类实现这个协议ViewControllerDelegate
@interface ViewController ()<ViewControllerDelegate>
@property (nonatomic, assign) id<ViewControllerDelegate> delegate;
 
@end

接着在-viewDidLoad中的代码如下,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- ( void )viewDidLoad
{
     [super viewDidLoad];
     // Do any additional setup after loading the view from its nib.
     self.delegate = self;
     if  (self.delegate && [self.delegate respondsToSelector:@selector(selfDelegateMethod)]) {
         [self.delegate selfDelegateMethod];
     }
}
 
#pragma mark - ViewControllerDelegate method
//实现协议中的方法
- ( void )selfDelegateMethod
{
     NSLog(@ "自己委托自己实现的方法" );
}

看出这种写法的奇葩地方了吗?自己委托自己去实现某个方法,而不是委托别的类去实现某个方法。本类中定义的一个Block其实就是闲的蛋疼,委托自己去字做某件事情,实际的意义不大,所以你很少看见别人的代码直接在类中定义Block然后使用的,Block很多的用处是跨越两个类来使用的,比如作为property属性或者作为方法的参数,这样就能跨越两个类了。

2、第二部分

__block关键字的使用

在Block的{}体内,是不可以对外面的变量进行更改的,比如下面的语句,

?
1
2
3
4
5
6
7
8
9
10
- ( void )viewDidLoad
{
     //将Block定义在方法内部
     int  x = 100;
     void  (^sumXAndYBlock)( int ) = ^( int  y){
     x = x+y;
     printf ( "new x value is %d" ,x);
     };
     sumXAndYBlock(50);
}

这段代码有什么问题呢,Xcode会提示x变量错误信息:Variable is not assigning (missing __block type),这时候给int x = 100;语句前面加上__block关键字即可,如下,

?
1
__block  int  x = 100;

这样在Block的{}体内,就可以修改外部变量了。

3、第三部分:Block作为property属性实现页面之间传值

需求:在ViewController中,点击Button,push到下一个页面NextViewController,在NextViewController的输入框TextField中输入一串字符,返回的时候,在ViewController的Label上面显示文字内容,

(1)第一种方法:首先看看通过“协议/代理”是怎么实现两个页面之间传值的吧,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//NextViewController是push进入的第二个页面
//NextViewController.h 文件
//定义一个协议,前一个页面ViewController要服从该协议,并且实现协议中的方法
@protocol NextViewControllerDelegate <NSObject>
- ( void )passTextValue:(NSString *)tfText;
@end
 
@interface NextViewController : UIViewController
@property (nonatomic, assign) id<NextViewControllerDelegate> delegate;
 
@end
 
//NextViewController.m 文件
//点击Button返回前一个ViewController页面
- (IBAction)popBtnClicked:(id)sender {
     if  (self.delegate && [self.delegate respondsToSelector:@selector(passTextValue:)]) {
         //self.inputTF是该页面中的TextField输入框
         [self.delegate passTextValue:self.inputTF.text];
     }
     [self.navigationController popViewControllerAnimated:YES];
}

接下来我们在看看ViewController文件中的内容,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//ViewController.m 文件
@interface ViewController ()<NextViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *nextVCInfoLabel;
 
@end
//点击Button进入下一个NextViewController页面
- (IBAction)btnClicked:(id)sender
{
     NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@ "NextViewController"  bundle:nil];
     nextVC.delegate = self; //设置代理
     [self.navigationController pushViewController:nextVC animated:YES];
}
 
//实现协议NextViewControllerDelegate中的方法
#pragma mark - NextViewControllerDelegate method
- ( void )passTextValue:(NSString *)tfText
{
     //self.nextVCInfoLabel是显示NextViewController传递过来的字符串Label对象
     self.nextVCInfoLabel.text = tfText;
}

这是通过“协议/代理”来实现的两个页面之间传值的方式。

(2)第二种方法:使用Block作为property,实现两个页面之间传值,

先看看NextViewController文件中的内容,

?
1
2
3
4
5
6
7
8
9
10
11
12
//NextViewController.h 文件
@interface NextViewController : UIViewController
@property (nonatomic, copy)  void  (^NextViewControllerBlock)(NSString *tfText);
 
@end
//NextViewContorller.m 文件
- (IBAction)popBtnClicked:(id)sender {
     if  (self.NextViewControllerBlock) {
         self.NextViewControllerBlock(self.inputTF.text);
     }
     [self.navigationController popViewControllerAnimated:YES];
}

再来看看ViewController文件中的内容,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
- (IBAction)btnClicked:(id)sender
{
     NextViewController *nextVC = [[NextViewController alloc] initWithNibName:@ "NextViewController"  bundle:nil];
     nextVC.NextViewControllerBlock = ^(NSString *tfText){
         [self resetLabel:tfText];
     };
     [self.navigationController pushViewController:nextVC animated:YES];
}
#pragma mark - NextViewControllerBlock method
- ( void )resetLabel:(NSString *)textStr
{
     self.nextVCInfoLabel.text = textStr;
}

好了就这么多代码,可以使用Block来实现两个页面之间传值的目的,实际上就是取代了Delegate的功能。

另外,博客中的代码Sample Code可以再Github下载,如果因为Github被墙了,可以在终端使用git clone + 完整链接,即可克隆项目到本地。

Github中的代码,可以开启两种调试模式,你需要在项目的配置文件BlockSamp-Prefix.pch注释或者解注释下面的代码,

?
1
#define Debug_BlcokPassValueEnable

即可开启两种调试的方式,如果注释了上面的语句就是使用Delegate进行调试;否则使用Block进行调试。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值