本文介绍了iPhone-modalViewController版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到释放模态视图控制器的正确方法.

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];

最后,父控制器的didDissmissModalView方法如下:

Finally, the didDissmissModalView method of parent controller is following:

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

(我使用ModalViewControllerDelegate协议,该协议需要实现该方法).

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

首先,我认为我应该在父控制器的dealloc方法中释放tipViewController:

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.

所以也许我应该在展示它之后释放tipViewController?

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];
}

假设self.modalViewController现在与tipViewController相同?

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

推荐答案

在调用presentModalViewController之后,应释放tipViewController:

You should release tipViewController after you call presentModalViewController:

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

这篇关于iPhone-modalViewController版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:41