2

I'm using multiple storyboards in my app, which is considered as good practice. For example, from my FooViewController in Main.storyboard, when a button is tapped, I'll jump to another BarViewController defined in Secondary.storyboard programmatically.

Transitions among storyboards and view controllers are implemented in a separate file, e.g. Storyboards, like the following:

// in Storyboards.m
@implementation Storyboards

+ (id)instantiateBarViewController {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    return [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
}

@end

Then in FooViewController, I did this:

// in FooViewController.m
- (IBAction)someButtonTapped:(id)sender {
    BarViewController *controller = [Storyboards instantiateBarViewController];
    [self.navigationController pushViewController:controller animated:YES];
}

This works just fine. But my questions are:

  1. Is it necessary to cache the storyboard instance, so that everytime instantiateBarViewController is invoked, I do not need to re-create the storyboard?
  2. If yes, should I cache BarViewController too?

To cache the storyboard (and the view controller), I could use code as the following:

// in Storyboards.m
@implementation Storyboards

+ (id)instantiateBarViewController {
    static UIViewController *barViewController = nil;  // Shared view controller.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        UIStoryboard *storyboard = [self secondaryStoryboard];
        barViewController = [storyboard instantiateViewControllerWithIdentifier:@"BarViewController"];
    });
    return barViewController;
}

+ (UIStoryboard *)secondaryStoryboard {
    static UIStoryboard *storyboard = nil;  // Shared storyboard.
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        storyboard = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    });
    return storyboard;
}

@end

Thanks!

4
  • 1
    This looks like a case of premature optimisation to me. Apr 17, 2015 at 8:25
  • I'd take a different approach and use: github.com/rob-brown/RBStoryboardLink it allows you to link storyboards visually. Very powerful tool Apr 17, 2015 at 8:28
  • @AshleyMills I have a doubt about that too, that's why I'm asking this question in SO before implementing it in my app. ;-)
    – zzheng
    Apr 17, 2015 at 8:29
  • @SimonMcLoughlin thanks for the link. I'm aware of this project too, but I'd prefer to use the manual way to do this. Because: 1) I think the code would be easier to understand; 2) as the project states: "RBStoryboardLink started out as a proof-of-concept and that is still largely the case." -- but if you think my manual approach has some serious flaw, or some obvious disadvantage, comparing to RBStoryboardLink, I'm glad to know and to switch to RBStoryboardLink.
    – zzheng
    Apr 17, 2015 at 8:33

2 Answers 2

6

I'd say "don't."

Keeping references to resources like this means you're stuffing things into memory on a memory-restricted device and never, ever letting them go. Bad developer! No cookie! ;-)

What evidence do you have that would suggest this is better than letting the system manage caching or trashing resources repeatedly loaded from xibs (which it does)? I'd say your approach is likely doing more harm than good. Remember, simply creating a storyboard instance doesn't directly translate to "loading it from the file system every time". The system manages this rather intelligently, so there's no need to short-circuit it by maintaining references that are never released.

If you're trying to solve some performance problem, I highly recommend using Instruments to measure performance and go from there. I seriously doubt it's a storyboard/xib loading issue.

3

A singleton seems overkill - all you need to instantiate a view controller from a storyboard is:

[[UIStoryboard storyboardWithName: @"<storyboard-name>" bundle: nil] instantiateViewControllerWithIdentifier: @"<identifier>"];

or

[[UIStoryboard storyboardWithName: @"<storyboard-name>" bundle: nil] instantiateInitialViewController];

or

[self.storyboard instantiateViewControllerWithIdentifier: @"<identifier>"];

Keep it simple!

2
  • 2
    Thanks for your answer! I upvoted it, but I'll accept the first answer, which is pretty much the same opinion as yours. :-)
    – zzheng
    Apr 17, 2015 at 14:44
  • Another upvote from the accepted answerer. I posted mine because I felt the need to elaborate. I'm long-winded. ;-) Apr 17, 2015 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.