parallax - addObserver:forKeyPath is crashing the app, KVO, objective c -
i doing parallax effect using category doing :
add , uiview uitableview (via category add addobserver:forkeypath whenever tableview moving, reframe view above details below uiscrollview+parallax.h
#import <uikit/uikit.h> @class parallaxview; @interface uiscrollview (parallax) @property (strong, nonatomic) parallaxview *parallaxview; - (void) addparallaxviewwith:(uiview*)parallaxview; - (void) removekvo; @end @interface parallaxview : uiview @end uiscrollview+parallax.m
static char parallaxkey; @implementation uiscrollview (parallax) @dynamic parallaxview; #pragma mark - add parallax view scrollview - (void) addparallaxviewwith:(parallaxview*)pview { if ( !self.parallaxview) { [self addsubview:pview]; [self setparallaxview:pview]; } } #pragma mark - set parallaxview + register parallaxview observer - (void) setparallaxview:(parallaxview *)parallaxview { objc_setassociatedobject(self, ¶llaxkey, parallaxview, objc_association_assign); /* these line crashing app */ // [self addobserver:self.parallaxview // forkeypath:@"contentoffset" // options:nskeyvalueobservingoptionnew // context:nil]; } #pragma mark - parallaxview - (parallaxview*) parallaxview { return (objc_getassociatedobject(self, ¶llaxkey)); } #pragma mark - remove - (void)removekvo { [self removeobserver:self.parallaxview forkeypath:@"contentoffset"]; } @end @implementation parallaxview -(id)init { //load xib main bundle , assign self self = [[[nsbundle mainbundle]loadnibnamed:@"parallex" owner:self options:nil] objectatindex:0]; return self; } -(id)initwithframe:(cgrect)frame { self = [self init]; [self setframe:frame]; return self; } ................ @end and adding parallax table doing
parallaxview *pview = [[parallaxview alloc]initwithframe:cgrectmake(0, 0, 320, 160)]; [self.tableview addparallaxviewwith:pview]; however, [self addobserver:forkeypath:options:context:nil] keeps crashing app without no clues @ all. if comments line out , app not crashing parallex effect not working.
any ideas problematics. please help. thanks
problem in code
-(id)initwithframe:(cgrect)frame { self = [self init]; [self setframe:frame]; return self; } in above code self = [self init]; , [self setframe:frame]; go in recursion give crash ,first fix guess solve problem,it should this
- (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { // initialization code } return self; } and loading view nib using
self = [[[nsbundle mainbundle]loadnibnamed:@"parallex" owner:self options:nil] objectatindex:0]; this code bad idea. can refer this task. happy , clean coding...
Comments
Post a Comment