如何在didReceiveMemoryWarning中使用storyboard卸载视图并重新创建它

时间:2013-04-04 07:45:39

标签: iphone ios objective-c storyboard

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

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

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

1 个答案:

答案 0 :(得分:0)

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

如果您选择释放视图,那么只有在它出现时才应该这样做  屏幕上目前无法显示

- (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;
    }
}

(来自View Controller Programming Guide for iOS的代码进行了一些小修改,以避免在当前卸载时加载视图。)

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