Search code examples
iphonememory-managementios6uistoryboard

Multiple Storyboards in iOS + Memory Management


I am mixing multiple storyboards alongside NIB files in my iOS6 / iOS6 app.

How does memory management work with multiple storyboards?

  • Upon launching a storyboardA, if I return to another nib file / go to another storyboard, does that storyboardA just stay there in the memory?
  • Am I able to get an "Array" of the storyboards in the stack the way I'm able to get the array of the views in a navigation controller's stack with UIViewControllers?
  • For memory management, can I pop or remove a storyboard from the memory? Do I set it to nil when I go to another storyboard?

reasons why I am using multiple storyboards mixed with nib files:

  • I want complex narrative storyboard flows. It doesn't make sense to slam them all into one place.
  • Eventually I'll work with many different people and I find the arguments for team work, version control compelling enough to opt for the multiple storyboard route
  • I'm mixing NIB files because storyboards do not yet work with complex nib files, many programmatic views, etc.

My Code Snippets

Inside InitialNibFile.m <-- that is launched from the appdelegate.

- (IBAction)newStoryboardBtnPressed:(id)sender {

[self.view removeFromSuperview]; // here I remove this view from the superview to save memory
UIStoryboard *newStoryboard = [UIStoryboard storyboardWithName:@"NewStoryboard" bundle:nil];
UIViewController *initialSettingsVC = [newStoryboard instantiateInitialViewController];

[self presentViewController:initialSettingsVC
                   animated:YES completion:nil];

 }

Here, Inside a UIViewController view that is within a storyboard scene.

- (IBAction)anotherStoryboardBtnPressed:(id)sender {

    // I'm inside a storyboard right now. I'm calling another storyboard. 
    // can i remove this storyboard before i launch the otherone? will keeping 2 or 3 toryboards in memory cause a memory leak?

    UIStoryboard *settingsStoryboard = [UIStoryboard storyboardWithName:@"AnotherStoryboard" bundle:nil];
    UIViewController *initialSettingsVC = [settingsStoryboard instantiateInitialViewController];
    // i'm going to load this view controller modally
    // initialSettingsVC.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:initialSettingsVC
                       animated:YES
                     completion:nil];

}

Solution

  • So here we go =)

    Upon launching a storyboardA, if I return to another nib file / go to another storyboard, does that storyboardA just stay there in the memory?
    A storyboard is retained by the UIViewController you create (via the storyboard) property. So yes, it does stay in memory until all of its view controllers are dealloc'd. Check out the (brief) documentation.

    Am I able to get an "Array" of the storyboards in the stack the way I'm able to get the array of the views in a navigation controller's stack with UIViewControllers?
    No, because storyboards are independent of each other. You can, however, get the storyboards by iterating over the view controllers in your nav stack.

    For memory management, can I pop or remove a storyboard from the memory? Do I set it to nil when I go to another storyboard?
    It'll stay around as long as you have an instantiated view controller in memory. When you use storyboardWithName: an autoreleased object is returned to you. The view controller will retain it, so do not worry about releasing or setting to nil.

    For references to your other questions, I defer to SO and the rest of the internet:
    Best Practices for Storyboards on SO
    UIStoryboard best practices
    Multiple Storyboards in iOS on SO

    Happy Storyboarding.