iphone - 如何使用didReceiveMemoryWarning和viewDidUnload?

标签 iphone cocoa-touch

使用这两种方法进行内存管理的最佳实践是什么?我当前的应用程序可以在内存警告后轻松释放其 View ,因为它需要大量内存。我需要在这些方法中做什么?我在应用程序中使用各种 Nib ,并将其中的对象连接到我的 View Controller 而不保留它们。从 Nib 加载 View 时是否需要保留 Nib 对象,为什么?我在文档中看到 OS 3.0 发生了变化,我只谈论 3.0

最佳答案

如果您只谈论 iOS 3.0,请在 viewDidUnload 中释放您可以轻松重新创建的任何对象。然后,您可以从 viewDidLoad 中的 Nib 重新创建或重新加载它们。确保当你释放对象时,要么将它们清零,要么让你的 setter 这样做。这样您就可以在重新加载或重新创建之前使用 viewDidLoad 检查是否为零。

对于从 nib 对象加载,如果您正在加载到属性中,并将它们设置为保留,则不必再次保留。

来自Apple :

The UIViewController class provides some automatic handling of low-memory conditions through its didReceiveMemoryWarning (page 20) method, which releases unneeded memory.

Prior to iPhone OS 3.0, this method was the only way to release additional memory associated with your custom view controller class but in iPhone OS 3.0 and later, the viewDidUnload (page 30) method may be a more appropriate place for most needs. When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad (page 29) method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

关于iphone - 如何使用didReceiveMemoryWarning和viewDidUnload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1460450/

相关文章:

iphone - 在 UITableView 单元格中格式化日期和时间

ios - SKStore​Review​Controller,如何正确使用?

iphone - 用于 iOS 聚合的 CoreData 或 SQLite

iphone - 出现键盘时滚动 Group tableView

iphone - 将 SDK 特定的框架添加到 Xcode

iphone - MPMoviePlayerController 进入黑屏

iphone - float UITableViewCell

cocoa-touch - 比较两个 NSStrings

iphone - 如何创建类似于 iOS 联系人应用程序的 UITableView?

ios - 避免触发 touchesBegan : until a swipe gesture recognizer fails