Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
372 views
in Technique[技术] by (71.8m points)

iPhone - modalViewController release

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

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

1 Reply

0 votes
by (71.8m points)

You should release tipViewController after you call presentModalViewController:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...