0

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

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

当我收到内存警告时,视图显示为黑色。如何从情节提要中恢复视图?

4

1 回答 1

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

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

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

于 2013-04-04T08:06:04.047 回答