Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try to find proper way of releasing modal view controller.

Basically, I have view controller, which presents modal view (fullscreen) after button is pressed.

TipViewController * tipViewController = [[TipViewController alloc] init];
tipViewController.delegate = self;
[self presentModalViewController:tipViewController animated:YES];   

Then, in modal view when it should be dissmissed I call:

[self.delegate didDismissModalView];

Finally, the didDissmissModalView method of parent controller is following:

- (void)didDismissModalView 
{
    // dismiss the modal view controller
    [self dismissModalViewControllerAnimated:YES];
}

(I use ModalViewControllerDelegate protocol, which requires to implement that method).

First I thought that I should release tipViewController in dealloc method of parent controller:

- (void)dealloc 
{
    [tipViewController release];
}

But then I saw, that it could be wrong way, because the modal view controller may be presented and dismissed many times before parent controller will be closed, and every time it would be allocated but only once released eventually.

So maybe I should release tipViewController just after presenting it?

TipViewController * tipViewController = [[TipViewController alloc] init];
tipViewController.delegate = self;
[self presentModalViewController:tipViewController animated:YES];
[tipViewController release];

Can I do this, althought the modal view is now displayed?

Or maybe I should release modal view that way:

- (void)didDismissModalView 
{
// dismiss the modal view controller
    [self dismissModalViewControllerAnimated:YES];
    [self.modalViewController release];
}

assuming that self.modalViewController is the same as tipViewController now?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
363 views
Welcome To Ask or Share your Answers For Others

1 Answer

You should release tipViewController after you call presentModalViewController:

 TipViewController * tipViewController = [[TipViewController alloc] init];
 tipViewController.delegate = self;
 [self presentModalViewController:tipViewController animated:YES];
 [tipViewController release];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...