協議(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】