iPhone - modalViewController 发布

发布于 2024-10-13 09:04:03 字数 1409 浏览 5 评论 0原文

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

基本上,我有视图控制器,它在按下按钮后呈现模式视图(全屏)。

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

然后,在模态视图中,当它应该被关闭时,我调用:

[self.delegate didDismissModalView];

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

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

我使用 ModalViewControllerDelegate 协议,它需要实现该方法)。

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

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

但后来我发现,这可能是错误的方式,因为模式视图控制器可能会在父控制器关闭之前多次呈现和关闭,并且每次都会关闭会被分配,但最终只会被释放一次。

所以也许我应该在展示完tipViewController 后立即发布它?

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

尽管现在显示了模式视图,我可以这样做吗?

或者也许我应该以这种方式发布模态视图:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

窗影残 2024-10-20 09:04:03

您应该在调用presentModalViewController后释放tipViewController:

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

You should release tipViewController after you call presentModalViewController:

 TipViewController * tipViewController = [[TipViewController alloc] init];
 tipViewController.delegate = self;
 [self presentModalViewController:tipViewController animated:YES];
 [tipViewController release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文