Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes UIViewController's presentModalViewController:animated: retain the modal controller?
    text
    copied!<p>This has implications on the way I interact with my modal controllers. When I first started out in iOS development, I assumed that <code>UIViewController</code> <em>did not</em> retain the modally presented view. Well, really it was more like I had no reason to assume it <em>did</em> retain them. This left me with fairly awkward attempts at releasing them when I knew they would have finished their dismissal animations:</p> <pre><code>_myViewController = [[UIViewController alloc] init]; [self. present modalViewController:_myViewController animated:YES]; /* Some stuff, then in a different method all together, probably as the result of a delegate callback or something... */ [self dismissModalViewControllerAnimiated:YES]; [_myViewController performSelector:@selector(release) withObject:nil afterDelay:0.5f]; </code></pre> <p>Then, I saw the <code>modalViewController</code> property of <code>UIViewController</code> and thought, "Man, I hope it retains that property when a modal view controller is presented." Sure enough, I logged the retain count on several of these attempts and noticed a general increase immediate after the call to <code>presentModalViewController:animated:</code> (I know, retain counts are not a perfect metric). So, somewhere along the line, I have started using a much nicer pattern where I assume that any controller object I present modally is retained by the presenting controller. This lets me write the standard present code:</p> <pre><code>UIViewController* myViewController = [[UIViewController alloc] init]; [self presentModalViewController:myViewController animated:YES]; [myViewController release]; // &lt;- Fire and forget! </code></pre> <p>Now, of course, there is no awkwardness: no need to wait for an animation to finish, or even keep a reference to the presented controller if I don't need it. I can blindly dismiss it later and not worry about leaking. I like it.</p> <p>I have logged many a dealloc in my modally presented controllers and they are always called precisely when I want, which leads me to feel confident in my approach: <code>UIViewController</code>'s <code>presentModalViewController:animated:</code> retains the presented controller as the <code>modalViewController</code> property.</p> <p>But, and this is the meat of this question, I realized that I can't confirm this as <strong><em>documented</em></strong> behavior. And if it's not documented, I should not feel nearly as safe as I do, because Apple makes no promises about the longevity of undocumented behavior. The <code>modalViewController</code> property is publicly <code>readonly</code>, so I can only assume a retain behind the scenes, and the documentation on <code>presentModalViewController:animated:</code> says only:</p> <blockquote> <p>Sets the modalViewController property to the specified view controller.</p> </blockquote> <p>"Sets" could be <code>assign</code> or <code>retain</code>. Nothing I read blatantly confirms or denies my position. Since this is an assumption I make often, I would really love it if someone could point out a fact that I have missed somewhere in the bowels of documentation to put my mind at ease about the legitimacy of this practice.</p> <p><strong>EDIT:</strong> In the ebb and flow of day-to-day life in the iOS SDK, I found myself in the header for UIViewController and started reading some of it. I gleaned some useful info that reminded me of this question and I decided to post it, in the event some future user stumbles upon this question and wants as much info as possible to satisfy their paranoia of a very standard practice. The little morsel is simply this, from the @interface ivar block in UIViewController.h:</p> <pre><code>UIViewController *_childModalViewController; </code></pre> <p>As opposed to these other declarations:</p> <pre><code>UIViewController *_parentViewController; // Nonretained NSHashTable *_childViewControllers; // Nonretained </code></pre> <p>The comments seem to explicitly state what is <em>not</em> retained. By virtue of a lack of comment on the modal view controller ivar declaration, it would seem it <em>is</em> retained.</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