iphone - Is - [UIView (MyOwnCategory) drawRect:] never called on 3.1.3? -
iphone - Is - [UIView (MyOwnCategory) drawRect:] never called on 3.1.3? -
i define own own drawrect method , called on 4.2.1 (ios) 5.0 (ios) , 4.3.2 (simulator) sucesfully. never called on 3.1.3 (iphone 2g).
what reason this?
p.s. since start write question think 3.1.3 device jailbroken. maybe root cause of unusual behaviour.
upd: reproduce issue utilize next code:
@implementation uiview (myowncategory) - (void)drawrect:(cgrect)rect { const char * function = __function__; [nsexception raise: @"hi!" format: @"%s", function]; } @end
exception never happened on 3.1.3 when phone call [super drawrect: rect]
explicitly
i wanted write method swizzling few weeks now, , @kevin ballard's comment made me (thank inspiration, kevin).
so here's solution problem using method swizzling should work on ios 3.x:
uiview+border.h:
#import <foundation/foundation.h> @interface uiview(border) @end
uiview+border.m:
#import "uiview+border.h" #import <quartzcore/quartzcore.h> #import <objc/runtime.h> @implementation uiview(border) - (id)swizzled_initwithframe:(cgrect)frame { // confusing part (article explains line). id result = [self swizzled_initwithframe:frame]; // safe guard: have uiview (or has layer)? if ([result respondstoselector:@selector(layer)]) { // layer view. calayer *layer = [result layer]; // set border on layer. layer.borderwidth = 2; layer.bordercolor = [[uicolor redcolor] cgcolor]; } // homecoming modified view. homecoming result; } - (id)swizzled_initwithcoder:(nscoder *)adecoder { // confusing part (article explains line). id result = [self swizzled_initwithcoder:adecoder]; // safe guard: have uiview (or has layer)? if ([result respondstoselector:@selector(layer)]) { // layer view. calayer *layer = [result layer]; // set border on layer. layer.borderwidth = 2; layer.bordercolor = [[uicolor bluecolor] cgcolor]; } // homecoming modified view. homecoming result; } + (void)load { // "+ load" method called once, in application life-cycle. // it's called before "main" function called. beware: there's no // autorelease pool @ point, avoid objective-c calls. method original, swizzle; // "- (id)initwithframe:" method. original = class_getinstancemethod(self, @selector(initwithframe:)); // "- (id)swizzled_initwithframe:" method. swizzle = class_getinstancemethod(self, @selector(swizzled_initwithframe:)); // swap implementations. method_exchangeimplementations(original, swizzle); // "- (id)initwithcoder:" method. original = class_getinstancemethod(self, @selector(initwithcoder:)); // "- (id)swizzled_initwithcoder:" method. swizzle = class_getinstancemethod(self, @selector(swizzled_initwithcoder:)); // swap implementations. method_exchangeimplementations(original, swizzle); } @end
iphone objective-c ios swizzling
Comments
Post a Comment