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

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

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

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

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

如果您select释放视图,那么只有在屏幕上当前不可见的情况下才应该这样做:

 - (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的一个小的修改,以避免加载视图,如果它是目前卸载。)

使用这段代码,视图会自动重新加载,如果它再次可见。