block的用法以及block和delegate的比較(轉寄),blockdelegate
看到一篇寫的關於block和delegate放在一起來方便大家理解的文章,感覺不錯,就推薦給大家來看一下。
代理設計模式對於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(nonatomic, assign) id<CustomCellDelegate> delegate;
@property (nonatomic, strong) UILabel *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 (nonatomic, strong) 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 alloc] initWithStyle: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 (nonatomic, strong) UILabel *text1Label;
@property (nonatomic, strong) UIButton *detailBtn;
//下面的定義,請看官們對比一下
/*delegate的定義 我沒有刪除,因為大家可以類比了看下*/
@property (nonatomic, assign) id<CustomCellDelegate> delegate;
/*這裡定義了ButtonBlock*/
@property (nonatomic, copy) void (^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 alloc] initWithStyle: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 alloc] init];
[self.navigationController pushViewController:detailVC animated:YES];
}
你們看這個方法是不是與CustomCellDelegate協議中的pushToNewPage方法類似。然後在回過頭來類比一樣,是不是block就是精簡版的delegate,因為delegate設計模式要寫協議CustomCellDelegate,還有容易遺漏cell.delegate = self;但是block使用的時候就簡單多了。