如何实现didReceiveMemoryWarning? [英] How to implement didReceiveMemoryWarning?

查看:113
本文介绍了如何实现didReceiveMemoryWarning?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个简单的位置感知iPhone应用程序,除了在手机内存不足的情况下,它在功能上非常符合我们的预期。

I have developed a simple location aware iPhone application which is functionally working very well to our expectations except in the low memory condition of the phone .

内存不足的情况手机我的应用程序崩溃了如果我通过释放一些空间来增加手机内存它再次开始运行良好而没有任何崩溃。

In low memory condition of the phone my app just crashes and If I increases the phone memory by freeing up some space it again start working well without any crash .

当我做了一些谷歌搜索问题我发现在内存不足的情况下,操作系统会将didReceiveMemoryWarning发送到当前层次结构中的所有控制器,这样每个控制器都应该实现didReceiveMemoryWarning方法,并为当前不可见的视图设置iboutlet为nil。

when I did some googling on the problem I found that in the low memory conditions the OS will send didReceiveMemoryWarning to all the controllers in the current hierarchy so that each one of them should implement didReceiveMemoryWarning method and also set iboutlet to nil for the view that is currently not visible .

我还读到某个地方,如果该控制器的视图不可见,将调用带有nil参数的方法 setView ,如果有的话一些附加到视图的出口变量会有移除它们的问题。

I have also read somewhere that if the view for that controller is not visible the method setView with nil parameter will be called and if there are some outlet variables attached to view there will be problem in removing them.

因此,所有这些基金会通过实施来处理Iphone引发的低级内存条件的最佳方法是什么 didReceiveMemoryWarning viewDidUnload 方法。

So with all these fundas what is the best to handle low level memory condition raised by the Iphone by implementing the didReceiveMemoryWarning and viewDidUnload methods.

如果可能,请提供正确的示例或链接以解决上述问题问题。

Please give a proper example or link if possible for the solution of the above problem .

谢谢。

推荐答案

我发布的一个例子。 ..我从somwhere复制过......它可能会给你一些想法......

One example i am posting...which i have copied from somwhere... it might give you some idea...

- (void)didReceiveMemoryWarning {

    // Release anything that's not essential, such as cached data (meaning
    // instance variables, and what else...?)

    // Obviously can't access local variables such as defined in method
    // loadView, so can't release them here We can set some instance variables
    // as nil, rather than call the release method on them, if we have defined
    // setters that retain nil and release their old values (such as through use
    // of @synthesize). This can be a better approach than using the release
    // method, because this prevents a variable from pointing to random remnant
    // data.  Note in contrast, that setting a variable directly (using "=" and
    // not using the setter), would result in a memory leak.
    self.myStringB = nil;
    self.myStringD = nil;
    [myStringA release];// No setter defined - must release it this way
    [myStringC release];// No setter defined - must release it this way

    /* 3. MUST CONFIRM: NOT necessary to release outlets here - See override of
       setView instead.
    self.labelA = nil;
    self.imageViewA = nil;
    self.subViewA = nil;
     */
    // Releases the view if it doesn't have a superview
    [super didReceiveMemoryWarning];
}

这篇关于如何实现didReceiveMemoryWarning?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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