日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

block的用法以及block和delegate的比较(转发)

發布時間:2024/1/17 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 block的用法以及block和delegate的比较(转发) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ?看到一篇寫的關于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使用的時候就簡單多了。

轉載于:https://www.cnblogs.com/MasterPeng/p/5210263.html

總結

以上是生活随笔為你收集整理的block的用法以及block和delegate的比较(转发)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。