iphone - pan gesture cause crash in subview? -


i create project single view application initial viewcontroller; add subviewcontroller project. uiimageview pan gesture added in both of view controllers.
works in viewcontroller.m, when added subviewcontroller , subview added subview view controller, program crash "exc_bad_access"..

anyone has solution?

here code:

viewcontroller.m

#import "viewcontroller.h" #import "subviewcontroller.h" //#define sub @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; #ifdef sub subviewcontroller *sb = [[subviewcontroller alloc] init]; [self.view addsubview:sb.view]; #else uiimageview* img_ = [[uiimageview alloc]initwithimage:[uiimage imagenamed:@"monkey_1.png"]]; img_.userinteractionenabled = yes; uipangesturerecognizer *stamppangesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)]; [stamppangesture setminimumnumberoftouches:1]; [stamppangesture setmaximumnumberoftouches:1]; [img_ addgesturerecognizer:stamppangesture]; img_.center = self.view.center; [self.view addsubview:img_]; #endif } - (void)handlepan:(uipangesturerecognizer *)recognizer { cgpoint translation = [recognizer translationinview:self.view]; recognizer.view.center = cgpointmake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer settranslation:cgpointmake(0, 0) inview:self.view]; } 

subviewcontroller.m

#import "subviewcontroller.h" @interface subviewcontroller () @end @implementation subviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; uiimageview* img_ = [[uiimageview alloc]initwithimage:[uiimage imagenamed:@"monkey_1.png"]]; img_.userinteractionenabled = yes; uipangesturerecognizer *stamppangesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)]; [stamppangesture setminimumnumberoftouches:1]; [stamppangesture setmaximumnumberoftouches:1]; [img_ addgesturerecognizer:stamppangesture]; img_.center = self.view.center; [self.view addsubview:img_]; } - (void)handlepan:(uipangesturerecognizer *)recognizer { cgpoint translation = [recognizer translationinview:self.view]; recognizer.view.center = cgpointmake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer settranslation:cgpointmake(0, 0) inview:self.view]; } 

assuming arc code, problem you're creating subview's view controller, grabbing it's view, letting controller fall out of scope , deallocated.

the immediate fix make subviewcontroller *sb instance variable of main view controller's class, way won't go out of scope , won't deallocated behind you.

thing is, while fix crash, bigger picture problem shouldn't grabbing view controller's view , adding subview, doing nothing controller itself. if that, example, various things might not work (e.g. rotation events never reach subview; ios needs communicate view controllers might not received subview's controller; don't know in world didreceivememorywarning situations do, etc.). in short, apple advises against this, , encourages keep view hierarchy , view controller hierarchy synchronized.

thus, if you're transitioning new view, should via pushviewcontroller or presentviewcontroller. in unlikely event subview (e.g. parts of prior view stay on screen , child view take portion of screen), can use view controller containment. can see wwdc 2011 session 102 more information on containment. that's undoubtedly overkill you're trying do. pushing/presenting subview logical solution.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -