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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

协议(Protocol)与委托代理(Delegate)

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 协议(Protocol)与委托代理(Delegate) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

協議(Protocol)的作用:

1. 規范接口,用來定義一套公用的接口;

2. 約束或篩選對象。

?

代理(Delegate):

它本身是一種設計模式,委托一個對象<遵守協議>去做某件事情,目的是為了降低對象間的耦合度;或用來逆向傳值。

?

一、定義一套公用接口

1 /** 協議 */ 2 @protocol ExecuteProtocol <NSObject> 3 4 @required 5 /** 6 * @brief 必須實現的某個方法 7 */ 8 - (NSUInteger)qualified; 9 10 11 @optional 12 /** 13 * @brief 可選的方法 14 */ 15 - (void)doSomething; 16 17 @end

協議只有.h文件,沒有.m文件。因為 Protocol 僅定義公用的一套接口,并不能提供具體的實現方法。(具體的實現需要某個遵守了協議的類去實現,然后該類就可以作為被篩選出來的對象做些事情,后面會講到)

?

假如控制器里面需要用到協議,那么導入協議:

?1 #import "ExecuteProtocol.h"?

并且實現協議的 required 方法(否則編譯器會warning)

?

ViewController的代碼如下:

1 #import "ViewController.h" 2 #import "ExecuteProtocol.h" 3 #import "Object.h" 4 5 @interface ViewController () 6 @property (nonatomic, strong) UILabel *label; 7 @end 8 9 @implementation ViewController 10 11 #pragma mark - View lifeCycle 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 [self.view addSubview:self.label]; 15 [self getHouse:[[Object alloc] init]]; 16 } 17 18 - (void)getHouse:(id <ExecuteProtocol>)obj { 19 self.label.text = [NSString stringWithFormat:@"%lu", [obj qualified]]; 20 } 21 22 #pragma mark - getter Methods 23 - (UILabel *)label { 24 if (!_label) { 25 _label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 26 _label.textAlignment = NSTextAlignmentCenter; 27 _label.backgroundColor = [UIColor redColor]; 28 } 29 return _label; 30 } 31 @end

在控制器里面添加一個方法,這個方法的參數必須是遵守了協議的某個對象,所以創建了Object對象:

1 #import <Foundation/Foundation.h> 2 #import "ExecuteProtocol.h" 3 4 /** 某對象 */ 5 @interface Object : NSObject <ExecuteProtocol> 6 7 @end

并且實現協議方法:

1 #import "Object.h" 2 3 @implementation Object 4 5 - (NSUInteger)qualified { 6 return 88; 7 } 8 9 @end

簡單的小Demo。

?

二、代理傳值(SecondaryViewController 傳值到 ViewController中)

1.在ViewController中

1 // ViewController需要 遵守代理 2 @interface ViewController () <SecondaryViewControllerDelegate> 3 4 SecondaryViewController *secVC = [[SecondaryViewController alloc] init]; 5 // 指定代理 6 secVC.delegate = self; 7 [self.navigationController pushViewController:secVC animated:YES];

1 // 實現required代理方法,實現傳值,打印結果 2 #pragma mark - SecondaryViewControllerDelegate Methods 3 - (void)controller:(SecondaryViewController *)controller text:(NSString *)text { 4 NSLog(@"%@ %@", controller, text); 5 }

?

2.在SecondaryViewController中

1)首先,聲明代理

1 #import <UIKit/UIKit.h> 2 @class SecondaryViewController; 3 4 /** 5 * @brief 協議方法(類名+Delegate) 6 */ 7 @protocol SecondaryViewControllerDelegate <NSObject> 8 @required 9 /** 10 * @brief 傳值 11 * 12 * @param controller 當前控制器 13 * @param text 文本值 14 */ 15 - (void)controller:(SecondaryViewController *)controller text:(NSString *)text; 16 @end 17 18 @interface SecondaryViewController : UIViewController 19 /** 20 * @brief 代理用weak修飾(防止內存泄露) 21 */ 22 @property (nonatomic, weak) id <SecondaryViewControllerDelegate> delegate; 23 @end

2)判斷代理存在與否和方法是否響應

1 /** 2 * SecondaryViewController 3 */ 4 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { 5 /** 6 * @brief 判斷是否設置了代理并且代理是否響應了代理方法 7 */ 8 if (self.delegate && [self.delegate respondsToSelector:@selector(controller:text:)]) { 9 [self.delegate controller:self text:@"傳值"]; 10 } 11 [self.navigationController popViewControllerAnimated:YES]; 12 }

源碼:戳這里

尊重作者勞動成果,轉載請注明: 【kingdev】

轉載于:https://www.cnblogs.com/xiu619544553/p/5295079.html

總結

以上是生活随笔為你收集整理的协议(Protocol)与委托代理(Delegate)的全部內容,希望文章能夠幫你解決所遇到的問題。

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