Protocol協(xié)議及委托代理Delegate?

  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。

前言:因?yàn)镺bject-C是不支持多繼承的,所以很多時(shí)候都是用Protocol(協(xié)議)來代替。Protocol(協(xié)議)只能定義公用的一套接口,但不能提供具體的實(shí)現(xiàn)方法。也就是說,它只告訴你要做什么,但具體怎么做,它不關(guān)心。

當(dāng)一個(gè)類要使用某一個(gè)Protocol(協(xié)議)時(shí),都必須要遵守協(xié)議。比如有些必要實(shí)現(xiàn)的方法,你沒有去實(shí)現(xiàn),那么編譯器就會(huì)報(bào)警告,來提醒你沒有遵守××協(xié)議。注意,我這里說的是警告,而不是錯(cuò)誤。對(duì)的,就算你不實(shí)現(xiàn)那些“必要實(shí)現(xiàn)”的方法,程序也是能運(yùn)行的,只不過多了些警告。

Protocol(協(xié)議)的作用:

一、定義一套公用的接口(Public)
  @required:必須實(shí)現(xiàn)的方法
 
  @optional:可選實(shí)現(xiàn)的方法(可以全部都不實(shí)現(xiàn))```
 
######二、委托代理(Delegate)傳值:
 
  它本身是一個(gè)設(shè)計(jì)模式,它的意思是委托別人去做某事。
 
  比如:兩個(gè)類之間的傳值,類A調(diào)用類B的方法,類B在執(zhí)行過程中遇到問題通知類A,這時(shí)候我們需要用到代理(Delegate)。
 
  又比如:控制器(Controller)與控制器(Controller)之間的傳值,從A1跳轉(zhuǎn)到A2,再從A2返回到A1時(shí)需要通知A1更新UI或者是做其它的事情,這時(shí)候我們就用到了代理(Delegate)傳值。


###一、定義一套公用的接口(Public)
 
 
 
 
 

//ProtocolDelegate.h代碼(協(xié)議不會(huì)生成.m文件):

import <Foundation/Foundation.h>

@protocol ProtocolDelegate <NSObject>

// 必須實(shí)現(xiàn)的方法
@required

  • (void)error;

// 可選實(shí)現(xiàn)的方法
@optional

  • (void)other;
  • (void)other2;
  • (void)other3;

@end```

在需要使用到協(xié)議的類,import它的頭文件:
#import "ViewController.h"
#import "ProtocolDelegate.h"```
 
 
我這里選擇的是入口文件
 
 

//記得要遵守協(xié)議:

@interface ViewController () <ProtocolDelegate>

@end




- 這時(shí)會(huì)報(bào)一個(gè)警告,因?yàn)槎x的協(xié)議里有一個(gè)是必須實(shí)現(xiàn)的方法,而我們沒有去實(shí)現(xiàn): 

```swift
實(shí)現(xiàn)了必須實(shí)現(xiàn)的方法后,編譯器就不會(huì)報(bào)警告了:

至于其它的可選方法,你可以選擇實(shí)現(xiàn),也可以全都不實(shí)現(xiàn)。```


###二、委托代理(Delegate)傳值

//ViewController.m文件:

import "ViewController.h"

import "ProtocolDelegate.h"

import "ViewControllerB.h"

@interface ViewController () <ProtocolDelegate, ViewControllerBDelegate>

@end

@implementation ViewController

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    ViewControllerB *vc = segue.destinationViewController;
    [vc setDelegate:self];
    }

// 這里實(shí)現(xiàn)B控制器的協(xié)議方法

  • (void)sendValue:(NSString *)value
    {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:value delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alertView show];
    }

  • (void)error
    {

}

@end

//ViewControllerB.h文件:

import <UIKit/UIKit.h>

// 新建一個(gè)協(xié)議,協(xié)議的名字一般是由“類名+Delegate”
@protocol ViewControllerBDelegate <NSObject>

// 代理傳值方法

  • (void)sendValue:(NSString *)value;

@end

@interface ViewControllerB : UIViewController

// 委托代理人,代理一般需使用弱引用(weak)
@property (weak, nonatomic) id<ViewControllerBDelegate> delegate;

@end

//ViewControllerB.m文件:

import "ViewControllerB.h"

@interface ViewControllerB ()

@property (strong, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewControllerB

  • (IBAction)backAction:(id)sender
    {
    if ([_delegate respondsToSelector:@selector(sendValue:)]) { // 如果協(xié)議響應(yīng)了sendValue:方法
    [_delegate sendValue:_textField.text]; // 通知執(zhí)行協(xié)議方法
    }
    [self.navigationController popViewControllerAnimated:YES];
    }

@end







###小結(jié):

``` swift
當(dāng)你需要定義一套公用的接口,實(shí)現(xiàn)方法可以是不同的時(shí)候,你可以使用Protocol協(xié)議。

當(dāng)你需要進(jìn)行類與類之間的傳值時(shí),你也可以基于Protocol協(xié)議,使用代理設(shè)計(jì)模式進(jìn)行傳值。 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容