Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];

}
like image 463
Romy Ilano Avatar asked Dec 06 '12 20:12

Romy Ilano


People also ask

Can we use multiple storyboards in one application?

Apple introduced them in iOS 9 and macOS 10.11. They do exactly what I needed. They allow you to break a storyboard up into multiple, smaller storyboards. A storyboard reference ties multiple storyboards together, creating one, large, composite storyboard.

How do I use multiple storyboards in Xcode?

On the main storyboard, drag and drop a Storyboard Reference from the object library into the storyboard, then Ctrl + click a button on the main controller -> drag it to the Storyboard Reference -> Select Show. Now we just created a segue between two storyboards.


3 Answers

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.

like image 166
MishieMoo Avatar answered Oct 23 '22 04:10

MishieMoo


It sounds like you're trying to avoid keeping unnecessary view controllers in memory. That's a great thing to avoid since view controllers are large in the sense that they references lots of other objects. Your question makes it sound as though you're worried that the starboard references all of the view controllers in the storyboard, and thus keeps all of them around too. That's not the case. The storyboard object is just a reference to the files in your resources directory of your app bundle, and every time it's asked to create a new view controller, it instantiates a new one from those resources. For example, it can instantiate the same template view controller from the storyboard multiple times. It doesn't hold on to any of the live view controllers after they're instantiated.

like image 41
Jon Hess Avatar answered Oct 23 '22 06:10

Jon Hess


It sounds like you're trying to avoid keeping unnecessary view controllers in memory

It’s important to realise that the storyboard is kept in memory for the lifetime of the application once it’s instantiated. If that isn’t what you want, then you can turn the static constant property into a static computed property.Create extention of your storyboard, same thing you can apply for your view controller so that make your application memory clean.

import UIKit

extension UIStoryboard {

static var main: UIStoryboard {
    return UIStoryboard(name: "Main", bundle: Bundle.main)
}

}

like image 43
Muhammad Shauket Avatar answered Oct 23 '22 06:10

Muhammad Shauket