Note that there are some explanatory texts on larger screens.

plurals
  1. POViewController added as a subView getting released too soon
    text
    copied!<p>I am trying to optimize the code in my app. I have quite a few ViewControllers which all use a common "keypad". I wanted to extract the keypad into a separate ViewController and then include it into the existing ViewControllers. This way I could obliviate duplicate code (which was needed to deal with the reactions from the keypad) in the separate ViewControllers.</p> <p>So in the KeyPadVC I have methods set up that look something like this.</p> <pre><code>-(IBAction)keyPadKeyPressed:(id)sender { [self.delegate interpretKeyPressed:[[sender titleLabel] text]]; } </code></pre> <p>In my "parent" ViewControllers I include the keypad by adding a subview to a plain UIView that I placed in Interface Builder (so that I have a visual placeholder) and hooked up to the variable <code>keypadView</code>.</p> <pre><code>-(void) viewDidLoad { [super viewDidLoad]; KeyPadViewController *kpVC = [[KeyPadViewController alloc] init]; [kpVC setDelegate: self]; [[self keypadView] addSubview:[kpVC view]]; } </code></pre> <p>This displays fine, but when I press a button on the KeyPadViewController I get a zombie object because the object was already released. I then tried to declare <code>KeyPadViewController *kpVC</code> in the <code>@interface</code> and tried a self instantiating method like: </p> <pre><code>-(KeyPadViewController *)kpVC { if (!kpVC) { kpVC = [[KeyPadViewController alloc] init]; } return kpVC; } </code></pre> <p>I obviously modified the <code>viewDidLoad</code> method, but the result was always the same. The object gets released too soon. If I add NSLogs I can see that <code>-(IBAction)keyPadKeyPressed</code> from the KeyPadVC never gets called, because it KeyPadVC was already released. </p> <p>What am I doing wrong? I am using ARC and iOS6 SDK.</p> <p>Thanks</p> <p>PS: this is pseudo-code to make things shorter - hope there are no typos - if so then that is not the issue. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload