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

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

问题描述

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

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.

我看到 UIViewController 有一个方法 unloadViewIfReloadable,它在内存警告出现时被调用.有人知道如何告诉 Cocoa Touch 我的视图不可重新加载吗?

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?

提前致谢

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

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

didReceiveMemoryWarning - 默认实现只释放视图如果它确定这样做是安全的所以

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

现在......我用一个空函数覆盖了 didReceiveMemoryWarning ,该函数只调用 NSLog 来让我知道收到了警告.但是 - 无论如何都会卸载视图.另外,究竟根据什么标准来决定视图是否可以安全卸载......哦!这么多问题!

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!

推荐答案

似乎对我有用的是覆盖 setView: 以忽略设置为 nil.它很笨拙,但是,这是一个笨拙的问题,这解决了问题:

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天全站免登陆