之前一直很好奇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;
。。。