iPhone - modalViewController 发布
我尝试找到释放模态视图控制器的正确方法。
基本上,我有视图控制器,它在按下按钮后呈现模式视图(全屏)。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在调用presentModalViewController后释放tipViewController:
You should release tipViewController after you call presentModalViewController: