问题
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?
回答1:
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;
}
}
(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.
来源:https://stackoverflow.com/questions/15805329/how-to-unload-view-and-recreate-it-using-storyboard-when-didreceivememorywarning