How to unload view and recreate it using storyboard when didReceiveMemoryWarning

给你一囗甜甜゛ 提交于 2019-12-12 03:05:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!