在 didReceiveMemoryWarning 时如何卸载视图并使用故事板重新创建它 [英] How to unload view and recreate it using storyboard when didReceiveMemoryWarning

查看:33
本文介绍了在 didReceiveMemoryWarning 时如何卸载视图并使用故事板重新创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了本教程 http://www.wannabegeek.com/?p=168 用于在不同的视图控制器(在我的故事板上)之间滑动.现在我想在收到 didReceiveMemoryWarning 时从视图控制器卸载视图.我试过这个:

I used this tutorial http://www.wannabegeek.com/?p=168 for swiping between different view controllers (which are on my storyboard). Now I want to unload the views from the view controllers when a didReceiveMemoryWarning is received. I've tried this:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    self.view = nil;
}

当我收到内存警告时,视图显示为黑色.如何从故事板中恢复视图?

And when I receive a memory warning, the view shows black. How can I recover the view from my storyboard?

推荐答案

请注意,您不必必须释放视图以响应内存警告.iOS 6 会自动释放后台视图使用的大部分资源.

Note that you don't have to release views in response to a memory warning. iOS 6 automatically releases most resources used by the views behind the scenes.

如果你选择释放视图,那么你应该只在它是当前在屏幕上不可见:

If you choose to release the view then you should do that only if it is currently not visible on screen:

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Add code to clean up any of your own resources that are no longer necessary.

    // If view is currently loaded, but not visible:
    if ([self isViewLoaded] && [self.view window] == nil)
    {
        // Add code to preserve data stored in the views that might be
        // needed later.

        // Add code to clean up other strong references to the view in
        // the view hierarchy.

        // Unload the view:
        self.view = nil;
    }
}

(代码来自 适用于 iOS 的视图控制器编程指南,稍加修改以避免在当前卸载时加载视图.)

(Code from View Controller Programming Guide for iOS with a small modification to avoid loading the view if it is currently unloaded.)

使用此代码,如果视图再次可见,应自动重新加载视图.

With this code, the view should automatically be reloaded if it becomes visible again.

这篇关于在 didReceiveMemoryWarning 时如何卸载视图并使用故事板重新创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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