MITMObject 1.0.1

MITMObject 1.0.1

TestsTested
LangLanguage Obj-CObjective C
License Custom
ReleasedLast Release Sep 2015

Maintained by Georgiy Malyukov.



MITMObject can be used to subclass Foundation classes with overriding system delegate methods.

Adding MITMObject to your project

Source files

Alternatively you can directly add the MITMObject.h and MITMObject.m source files to your project.

  1. Download the latest code version or add the repository as a git submodule to your git-tracked project.
  2. Open your project in Xcode, then drag and drop MITMObject.h and MITMObject.m onto your project (use the "Project Navigator view"). Make sure to select Copy items when asked if you extracted the code archive outside of your project.
  3. Include MITMObject wherever you need it with #import "MITMObject.h".

Usage

At first you should subclass any another class with delegate methods inside which you want to override. For example, let's override UIScrollView class:

// MyScrollView.h

#import <UIKit/UIKit.h>

@interface MyScrollView : UIScrollView

@end

// MyScrollView.m

#import "MyScrollView.h"

@implementation MyScrollView

@end

Next, add MITMObject class instance to your subclass private section, like this:

// MyScrollView.m

#import "MyScrollView.h"
#import "MITMObject.h"

@interface MyScrollView () {
    MITMObject *mitm;
}

@end

@implementation MyScrollView

@end

Then you should implement an initializer which you will use and call MITM_INIT(mitm) macro inside it. For a UIScrollView subclass which will be used in Interface Builder it's necessary to implement initWithCoder: initializer. Also call MITM_IMPLEMENT(mitm) macro right before writing any implementation logic. Resulting code may look like this:

// 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

After that you are free to override any delegate methods from your parent class, just don't forget to make forward calls in your own implementations. For example, overridden scrollViewDidScroll: method may look like this:

// 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

You can find sample application in the Samples folder.

License

Apache. See LICENSE for details.