MITMObject 可以用于使用重写系统代理方法的基类进行子类化。
或者您可以直接将 MITMObject.h
和 MITMObject.m
源文件添加到您的项目中。
MITMObject.h
和 MITMObject.m
拖放到您的项目(使用“项目导航视图”)。确保在选择时勾选“拷贝项目”。#import "MITMObject.h"
在需要 MITMObject 的地方包含它。首先,您应该子类化任何包含委托方法的其他类,其中您想重写。例如,让我们重写 UIScrollView 类
// MyScrollView.h
#import <UIKit/UIKit.h>
@interface MyScrollView : UIScrollView
@end
// MyScrollView.m
#import "MyScrollView.h"
@implementation MyScrollView
@end
接下来,将 MITMObject 类实例添加到您的子类私有部分,如下所示
// MyScrollView.m
#import "MyScrollView.h"
#import "MITMObject.h"
@interface MyScrollView () {
MITMObject *mitm;
}
@end
@implementation MyScrollView
@end
然后,您应该实现一个初始化器,您将使用并在其中调用 MITM_INIT(mitm)
宏。对于一个将在 Interface Builder 中使用的 UIScrollView 子类,有必要实现 initWithCoder:
初始化器。同时,在编写任何实现逻辑之前,调用 MITM_IMPLEMENT(mitm)
宏。生成的代码可能看起来像这样
// MyScrollView.m
#import "MyScrollView.h"
#import "MITMObject.h"
@interface MyScrollView () {
MITMObject *mitm;
}
@end
@implementation MyScrollView
MITM_IMPLEMENT(mitm);
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
MITM_INIT(mitm);
// your own initializations...
// ...
}
return self;
}
@end
然后您可以自由地重写父类中的任何委托方法,只是在您自己的实现中不要忘记进行正向调用。例如,重写的 scrollViewDidScroll:
方法可能看起来像这样
// MyScrollView.m
#import "MyScrollView.h"
#import "MITMObject.h"
@interface MyScrollView () {
MITMObject *mitm;
}
@end
@implementation MyScrollView
MITM_IMPLEMENT(mitm);
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
MITM_INIT(mitm);
// your own initializations...
// ...
}
return self;
}
#pragma mark - MITM Overrides
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// your own implementation
// ...
// forward call
if ([self.delegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[self.delegate scrollViewDidScroll:scrollView];
}
}
@end
您可以在 Samples
文件夹中找到示例应用程序。
Apache。请参阅 LICENSE
获取详细信息。