忙着考试,实验室的事 。一直没机会学习自己的iOS,哎哎哎哎哎。自己写了个demo,加深下理解吧。
#import "ViewController+swiss.h"
#import <objc/runtime.h>
@implementation ViewController (swiss)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL viewWillAppearSelector = @selector(viewWillAppear:);
SEL tourGuideWillAppearSelector = @selector(tourGuideWillAppear:);
Method originalMethod = class_getInstanceMethod(self, viewWillAppearSelector);
Method newMethod = class_getInstanceMethod(self, tourGuideWillAppearSelector);
BOOL methodAdded = class_addMethod([self class],
viewWillAppearSelector,
method_getImplementation(newMethod),
method_getTypeEncoding(newMethod));
if (methodAdded) {
class_replaceMethod([self class],
tourGuideWillAppearSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, newMethod);
}
});
}
- (void)tourGuideWillAppear:(BOOL)animated
{
NSLog(@"oooooooooooooooooo");
[self tourGuideWillAppear:animated];
}
@end