iphone - Override method using runtime library -
i need override - (void)viewwillappear:(bool)animatedfor viewcontrollers adding nslog(@"blabla") in method. i.e. after every call of viewwillappear invokes implemented realization of viewwillappear + nslog message. possible? if yes, please give me advice.
currently have tried code
@implementation runtimetest imp previusimp; imp newimp; - (void)ovverrideviewwillappearinviewcontroller:(class)vcclass { newimp = class_getmethodimplementation([self class], @selector(viewwillappear:)); method viewwillappearmethod = class_getinstancemethod(vcclass, @selector(viewwillappear:)); previusimp = method_setimplementation(viewwillappearmethod, newimp); } - (void)viewwillappear:(bool)animated { previusimp(self, @selector(viewwillappear:), animated); nslog(@"log2"); } @end then have
@implementation irviewcontroller2 - (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; nslog(@"log"); } @end my custom viewwillappear invokes first, viewwillappear irviewcontroller2. , after application crashes exc_bad_access. what's wrong?
if can't use subclass because want globally, don't want lose old version of method (so category override out of question) need swizzle away old method, put in new method calls old method... have used called jrswizzle perform in past, makes easy swizzle without writing bunch of runtime code.
if (![self jr_swizzlemethod:@selector(originalinit) withmethod:@selector(init) error:&err]) { nslog(@"unable jr_swizzle methods, error: %@",err); exit(exit_failure); } if (![self jr_swizzlemethod:@selector(init) withmethod:@selector(myinit) error:&err]) { nslog(@"unable jr_swizzle methods, error: %@",err); exit(exit_failure); } then -myinit method can call -originalinit or whatever methods using.
Comments
Post a Comment