iPhone - modalViewController release

百般思念 提交于 2019-12-25 08:13:02

问题


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?


回答1:


You should release tipViewController after you call presentModalViewController:

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


来源:https://stackoverflow.com/questions/4759427/iphone-modalviewcontroller-release

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!