views:

72

answers:

2

Hi,

I have been studying how to display a modal view with UIAlertView for a few hours and I understood that showing it does not "block" the code (the modal window is displayed and the program keeps running - we must use delegate to catch the selected actions on this modal window). Then I studied several examples and noticed that every example always release the modal window just after showing it. How can this work properly since the view will be released instantly as the code does not stop ?

Here is the example (there are many others on Google):

  [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message..." delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:NULL];  
  [alert showModal];  
  [alert release];

Thanks for your help, Apple 92

+2  A: 

The alloc method will return you an instance that has a retain count of 1. The showModal method probably retains the alert view so it remains on screen (and retained) until a button is tapped. It makes sense to me, since you are presenting it as a modal window, so it doesn't have a "parent", that is responsible of releasing it.

pgb
Yes ! If we suppose that showModal retains, then the retain counter will be increased by 1, then valuing 2. And release will decrease by 1 to 1. Then after the release, we still have a counter valuing 1, then the windows will not be deallocated...
But it's released again when the alert is dismissed. It is ok that the alert has a retain count of 1 while it is shown.
pgb
+2  A: 

I'm not sure where you're getting -showModal from (the usual method is just -show), but that act adds the alert to the view hierarchy. When a view is added as a subview of another view (I believe in this case it's a system-level view that is being added to) it's retained automatically, so you don't have to.

Ben Gottlieb
Yes, that is -show, not -showModal.