0

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 Answers1

0

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.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I've put this in all my view controllers didReceiveMemoryWarning method, but when I simulate a memory warning, it seems like nothing happens. If I look at the memory it drops 0.27MB and I got 75 viewControllers... –  Apr 04 '13 at 08:23
  • 1
    @nonuma: I don't know if I can help you with that. Does it fix the "view shows black" problem? - You could add an NSLog or breakpoint to `viewDidLoad` to verify that the view is actually reloaded (and therefore was unloaded before). – Martin R Apr 04 '13 at 08:28
  • I've put an NSLog in viewDidLoad, when I simulate a memory warning it doesn't print that out, only "Received memory warning" –  Apr 04 '13 at 08:36
  • 1
    @nonuma: The view should be unloaded if it is 1) currently loaded and 2) currently not visible. - The view should be reloaded (viewDidLoad) if it becomes visible again. – Martin R Apr 04 '13 at 08:51
  • Maybe its because I've used the tutorial that I said before, all the viewcontrollers are added as child in the beginning –  Apr 04 '13 at 08:54
  • yeah, its because the viewcontrollers are all getting loaded like in the tutorial... –  Apr 04 '13 at 09:15
  • @nonuma: OK, I gave just a general answer. I might look at your tutorial later, when I have more time. – Martin R Apr 04 '13 at 09:19
  • @nonuma: You are right, this method does not work with the view controllers from that tutorial. I *assume* that the problem is that all view controllers are added as child view controllers, and that it would work better if only the currently visible view controllers are added, and the other view controllers removed from the container view controller. But that is a pure guess. Sorry that I could not help more. – Martin R Apr 04 '13 at 20:41