iOS delegate繼承(協(xié)議、代理的繼承)

之前一直很好奇UITableView有一個delegate屬性,遵守 <UITableViewDelegate>協(xié)議的 ,而它的父類UIScrollView也有一個同名的delegate,但是遵守的是<UIScrollViewDelegate>協(xié)議。這是怎么實現(xiàn)的呢?

先上代碼

.h文件

#import <UIKit/UIKit.h>
@class CustomTextView;

// 創(chuàng)建自己的協(xié)議,同時遵守于父類協(xié)議(也就相當于繼承子父類協(xié)議,實際上協(xié)議本身是不能被繼承的,遵守就相當于抄了一份過來吧)
@protocol CustomTextViewDelegate <UITextViewDelegate>
// 這里添加自己在原協(xié)議方法基礎上,新增的一些協(xié)議方法
// 示例:
- (void)customTextView:(CustomTextView *)customTextView someNewAction:(BOOL)action;
@end

@interface CustomTextView : UITextView
// 因為delegate此時要遵守我們新創(chuàng)建的協(xié)議,而不是原本的協(xié)議,所以要重寫父類中的屬性(這里會有警告,稍后會在.m文件中消除)
@property (nonatomic, weak) id<CustomTextViewDelegate> delegate;
@end

.m文件

#import "CustomTextView.h"

@implementation CustomTextView
@dynamic delegate;// .h中警告說delegate在父類已經(jīng)聲明過了,子類再聲明也不會重新生成新的方法了。我們就在這里使用@dynamic告訴系統(tǒng)delegate的setter與getter方法由用戶自己實現(xiàn),不由系統(tǒng)自動生成

#pragma Set & Get
- (void)setDelegate:(id<CustomTextViewDelegate>)delegate
{
  super.delegate = delegate;
}

- (id<CustomTextViewDelegate>)delegate
{
  id curDelegate = super.delegate;
  return curDelegate;
}

// 找個地方觸發(fā)下新加的協(xié)議方法,看是否正常工作
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  {
    if ([self.delegate respondsToSelector:@selector(customTextView:someNewAction:)]) {
      [self.delegate customTextView:self someNewAction:YES];
  }
}

上面就是最簡單的:繼承父類協(xié)議,然后在其基礎上增加新的協(xié)議方法

然而我們更常見的需求是,需要對父類中的協(xié)議方法做修改,比如在父類的某協(xié)議調(diào)用前插入一些操作,然后再調(diào)用代理(或者在調(diào)用父類某協(xié)議后,再插入一些操作)

思路是:將父類的delegate指向自己,自己實現(xiàn)父類中的協(xié)議方法,并在其中做修改,然后在適當?shù)臅r間調(diào)用自己的delegate,將事件調(diào)用傳遞出去

通俗些說:有個作者告訴編輯說:“我的文章寫好了”(通過作者的delegate告訴編輯),然后編輯說:“我要校審下這個文章,改一改”(編輯實現(xiàn)了作者的delegate中的方法,在其內(nèi)做修改),最后編輯告訴一個讀者:“文章改好了,你可以去讀了”(最后通過編輯的delegate將事件傳遞出去)

需要修改父類協(xié)議的做法

.h文件的代碼上面一樣

#import <UIKit/UIKit.h>
@class CustomTextView;

// 創(chuàng)建自己的協(xié)議,同時遵守于父類協(xié)議(也就相當于繼承子父類協(xié)議,實際上協(xié)議本身是不能被繼承的,遵守就相當于抄了一份過來吧)
@protocol CustomTextViewDelegate <UITextViewDelegate>
// 這里添加自己在原協(xié)議方法基礎上,新增的一些協(xié)議方法
// 示例:
- (void)customTextView:(CustomTextView *)customTextView someNewAction:(BOOL)action;
@end

@interface CustomTextView : UITextView
// 因為delegate此時要遵守我們新創(chuàng)建的協(xié)議,而不是原本的協(xié)議,所以要重寫父類中的屬性(這里會有警告,稍后會在.m文件中消除)
@property (nonatomic, weak) id<CustomTextViewDelegate> delegate;
@end

.m文件

#import "CustomTextView.h"

@interface CustomTextView () <UITextViewDelegate> // 這里要遵守父類原本的協(xié)議,因為繼承類時,協(xié)議是不會被繼承的,所以需要重新聲明
@property (nonatomic, weak) id<CustomTextViewDelegate> myDelegate;// 因為父類的delegate對象是self,self的delegate是外部類,所以需要新增一個myDelegate來保存外部類
@end

@implementation CustomTextView
@dynamic delegate;// .h中警告說delegate在父類已經(jīng)聲明過了,子類再聲明也不會重新生成新的方法了。我們就在這里使用@dynamic告訴系統(tǒng)delegate的setter與getter方法由用戶自己實現(xiàn),不由系統(tǒng)自動生成

#pragma mark - Set & Get
- (void)setDelegate:(id<CustomTextViewDelegate>)delegate
{
    super.delegate = self;// 這句也可以放在初始化方法中
    _myDelegate = delegate;
}

- (id<CustomTextViewDelegate>)delegate
{
    return _myDelegate;
}

#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    BOOL result = NO;

    /*
    調(diào)用前可以做一些操作
    。。。
    */

    // 將協(xié)議事件傳遞出去
    if ([_myDelegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
        result = [_myDelegate textViewShouldBeginEditing:self];
    }

    /*
     調(diào)用后也可以做一些操作
     。。。
    */

    // 可以在達到某個條件的情況下,調(diào)用新增的協(xié)議方法
    if ([_myDelegate respondsToSelector:@selector(customTextView:someNewAction:)]) {
        [_myDelegate customTextView:self someNewAction:YES];
    }

    return result;
 }

/*
 建議將原協(xié)議的所有方法都實現(xiàn),不需要插入操作的就直接用_myDelegate傳遞出去
 */
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
  BOOL result = NO;
  if ([_myDelegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
      result = [_myDelegate textViewShouldEndEditing:self];
  }
  return result;
}

// 剩余方法類似
- (void)textViewDidBeginEditing:(UITextView *)textView;
- (void)textViewDidEndEditing:(UITextView *)textView;
。。。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,040評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,563評論 19 139
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內(nèi)部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,697評論 18 399
  • 原來我非不快樂 始2016.6.27 聽說,能到達金字塔頂端的只有兩種動物,一是雄鷹,靠著自己的...
    _行走中的蝸牛_閱讀 1,670評論 1 8
  • 感恩是一種回饋,對愛的回饋。 當你感恩時,就是對周圍的人或事表示“我收到了,謝謝你的愛”。 其實,周圍世界帶給我們...
    自我覺察日記本閱讀 220評論 0 0

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