ios6 - ViewController added as a subView getting released too soon -
i trying optimize code in app. have quite few viewcontrollers use common "keypad". wanted extract keypad separate viewcontroller , include existing viewcontrollers. way obliviate duplicate code (which needed deal reactions keypad) in separate viewcontrollers.
so in keypadvc have methods set this.
-(ibaction)keypadkeypressed:(id)sender { [self.delegate interpretkeypressed:[[sender titlelabel] text]]; } in "parent" viewcontrollers include keypad adding subview plain uiview placed in interface builder (so have visual placeholder) , hooked variable keypadview.
-(void) viewdidload { [super viewdidload]; keypadviewcontroller *kpvc = [[keypadviewcontroller alloc] init]; [kpvc setdelegate: self]; [[self keypadview] addsubview:[kpvc view]]; } this displays fine, when press button on keypadviewcontroller zombie object because object released. tried declare keypadviewcontroller *kpvc in @interface , tried self instantiating method like:
-(keypadviewcontroller *)kpvc { if (!kpvc) { kpvc = [[keypadviewcontroller alloc] init]; } return kpvc; } i modified viewdidload method, result same. object gets released soon. if add nslogs can see -(ibaction)keypadkeypressed keypadvc never gets called, because keypadvc released.
what doing wrong? using arc , ios6 sdk.
thanks
ps: pseudo-code make things shorter - hope there no typos - if not issue. :)
keypadviewcontroller *kpvc = [[keypadviewcontroller alloc] init]; [kpvc setdelegate: self]; [[self keypadview] addsubview:[kpvc view]]; self.kpvc = kpvc; that retains view controller.
however, doing totally illegal because not using parent-child architecture. see answer here: https://stackoverflow.com/a/15962125/341994
you can add subview view, must not add view controller's view subview view without going through elaborate parent-child architecture. , not doing that.
i explain parent-child architecture here:
http://www.apeth.com/iosbook/ch19.html#_container_view_controllers
Comments
Post a Comment