UIPopoverController和内存管理 [英] UIPopoverController and memory management

查看:64
本文介绍了UIPopoverController和内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    UIPopoverController *historyPop = [[UIPopoverController alloc] initWithContentViewController:nav];
    [nav release];
    [historyPop setPopoverContentSize:CGSizeMake(400, 500)];
    [historyPop presentPopoverFromRect:CGRectMake(button.frame.origin.x, button.frame.origin.y, button.frame.size.width, 5) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
    //[historyPop release];

这是我当前的代码,但分析师说这可能是泄漏,有(因为释放线被注释掉了)。但是,如果我取消注释释放线,那么应用程序崩溃并且说当dealloc仍然可见时,弹出窗口上的dealloc,那么我应该何时释放弹出控制器?

This is my current code, however the Analyser says that the is probably a leak, which there is (as the release line is commented out). But if I uncomment the release line then the app crashes and says dealloc was reach on the popover while it is still visible, so when exactly should I be releasing the popover controller?

推荐答案

正如在几个地方所提到的,呈现弹出窗口的方法(从矩形或工具栏按钮)不会保留弹出窗口。因此,您的呈现视图控制器需要保留对它的引用并在适当的时间释放它。

As mentioned in several places, the methods that present a popover (either from a rect, or from a toolbar button) do not retain the popover. So, your presenting view controller needs to hold a reference to it and release it at the appropriate time.

您可以通过将呈现视图控制器设置为popover的委托来完成此操作, 如上所述。一种更简单(如果内存效率稍低)的方法是声明一个retain属性来保存UIPopoverController。创建弹出窗口时,将其分配给保留它的属性。如果您稍后创建另一个弹出窗口,它将在您重新分配属性时释放上一个弹出窗口。不要忘记在呈现视图控制器的dealloc方法(以及viewDidUnload)中释放该属性。

You can do this by setting the presenting view controller as the popover's delegate, as mentioned. A simpler, if slightly less memory-efficient, approach is to declare a retain property to hold the UIPopoverController. When you create the popover you assign it to the property, which retains it. If you later create another popover, it will release the previous popover when you reassign the property. Don't forget to release the property in the presenting view controller's dealloc method (as well as viewDidUnload).

这种方法不会泄漏,您也不需要与代理人打交道。但是,您可能会将UIPopoverController对象保留的时间超过必要的时间。您可以自行决定是否对您的应用程序感兴趣。

This approach won't leak, and you don't need to deal with delegates. But, you will potentially keep a UIPopoverController object around longer than necessary. It's up to you to determine if that's a concern for your app.

这篇关于UIPopoverController和内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆