gpt4 book ai didi

iphone - 如何使用didReceiveMemoryWarning和viewDidUnload?

转载 作者:行者123 更新时间:2023-12-03 19:10:05 25 4
gpt4 key购买 nike

使用这两种方法进行内存管理的最佳实践是什么?我当前的应用程序可以在内存警告后轻松释放其 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com