You can do this, but without using a category. Category replaces the method. (Warning, car analogy) If you have a car and you destroy this car and replace it with a new car, can you use an old car? No, because he left and no longer exists. Same thing with categories.
What you can do is use the Objective-C runtime to add a method with a different name at runtime (say " bogusHitTest:withEvent: "), and then change the implementations of hitTest:withEvent: and bogusHitTest:withEvent: That way, when the code calls hitTest:withEvent: it will execute the code that was originally written for bogusHitTest:withEvent: Then you can call this code bogusHitTest:withEvent: which will do the initial implementation.
Thus, the dummy method will look like this:
- (UIView *) bogusHitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"executing: %@", NSStringFromSelector(_cmd)); return [self bogusHitTest:point withEvent:event]; }
The code for replacing the methods will look like this:
Method bogusHitTest = class_getInstanceMethod([UIView class], @selector(bogusHitTest:withEvent:)); Method hitTest = class_getInstanceMethod([UIView class], @selector(hitTest:withEvent:)); method_exchangeImplementations(bogusHitTest, hitTest);
Dave delong
source share