MITMObject 1.0.1

MITMObject 1.0.1

测试已测试
语言语言 Obj-CObjective C
许可证 自定义
发布最后发布2015 年 9 月

Georgiy Malyukov 维护。



MITMObject 可以用于使用重写系统代理方法的基类进行子类化。

将 MITMObject 添加到您的项目中

源文件

或者您可以直接将 MITMObject.hMITMObject.m 源文件添加到您的项目中。

  1. 下载最新的代码版本 [点击这里下载],或者将仓库添加为 git 子模块到您的 git 跟踪的项目中。
  2. 在 Xcode 中打开您的项目,然后将 MITMObject.hMITMObject.m 拖放到您的项目(使用“项目导航视图”)。确保在选择时勾选“拷贝项目”。
  3. 使用 #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 获取详细信息。