UIViewController防止视图卸载 [英] UIViewController prevent view from unloading

查看:98
本文介绍了UIViewController防止视图卸载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的iPhone应用程序收到一个内存警告,当前不可见的UIViewControllers的视图被卸载。在一个特定的控制器卸载视图和出口是相当致命的。



我正在寻找一种方法来防止此视图被卸载。我发现这种行为相当愚蠢 - 我有一个缓存机制,所以当内存警告来临 - 我卸载了大量的数据,我释放足够的内存,但我肯定需要这个视图不变。



我看到UIViewController有一个方法 unloadViewIfReloadable ,当内存警告来时调用。有人知道如何告诉Cocoa Touch我的视图不能重新加载?



任何其他建议如何防止我的观点在内存警告时被卸载?



p>




Apple文档关于视图控制器的视图生命周期说:



< blockquote>

didReceiveMemoryWarning - 默认的
实现释放视图
如果它确定可以安全地执行
所以


< blockquote>

现在...我用一个空函数覆盖 didReceiveMemoryWarning ,它只调用NSLog来让我知道收到警告。但是 - 视图被卸载反正。此外,在什么标准完全决定一个视图是否可以安全卸载...哦!这么多问题!

解决方案

似乎对我有用的是重写 setView:忽略设置为nil。它是kludgy,但是,然后,这是一个kludgy问题,这做了诀窍:

   - (void)setView: *)view {
if(view!= nil || self.okayToUnloadView){
[super setView:view];
}
}


When my iPhone app receives a memory warning the views of UIViewControllers that are not currently visible get unloaded. In one particular controller unloading the view and the outlets is rather fatal.

I'm looking for a way to prevent this view from being unloaded. I find this behavior rather stupid - I have a cache mechanism, so when a memory warning comes - I unload myself tons of data and I free enough memory, but I definitely need this view untouched.

I see UIViewController has a method unloadViewIfReloadable, which gets called when the memory warning comes. Does anybody know how to tell Cocoa Touch that my view is not reloadable?

Any other suggestions how to prevent my view from being unloaded on memory warning?

Thanks in advance


Apple docs about the view life cycle of a view controller says:

didReceiveMemoryWarning - The default implementation releases the view only if it determines that it is safe to do so

Now ... I override the didReceiveMemoryWarning with an empty function which just calls NSLog to let me know a warning was received. However - the view gets unloaded anyway. Plus, on what criteria exactly is decided whether a view is safe to unload ... oh ! so many questions!

解决方案

What appears to be working for me was to override setView: to ignore setting to nil. It's kludgy, but then, this is a kludgy issue, and this did the trick:

-(void)setView:(UIView*)view {
    if(view != nil || self.okayToUnloadView) {
        [super setView:view];
    }
}

这篇关于UIViewController防止视图卸载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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