Using Multiple Storyboards in iOS Using Multiple Storyboards in iOS ios ios

Using Multiple Storyboards in iOS


These are the best articles I've seen on multiple storyboards.

Not only does this guy tell you how to create a new storyboard in code, he

  • recommends multiple storyboards in practice (more modular code)
  • discusses when to use xibs vs storyboards (xibs hold views, storboards are based on controllers)
  • provides a class for linking storyboards with segues on github

Note that this last point is important because the key downside of multiple storyboards is that you can't usually link them with segues, but robs library allows that with a bit of fudging

Also see the discussed here


The OP edited his question to include the answer:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"HelpStoryboard" bundle:nil];UIViewController* initialHelpView = [storyboard instantiateInitialViewController];initialHelpView.modalPresentationStyle = UIModalPresentationFormSheet;[self presentModalViewController:initialHelpView animated:YES];

Of course, where you call this from is meaningful, because you might have 2 storyboards and their view stack in memory. So probably best if you call this code from outside the other storyboard's view controllers.


I've examined the RBStoryboardLink approach suggested by Rhubarb. This implementation substitutes view controller's properties which looks odd. I believe I've found the way to avoid this. Here is the demo project.

Navigation controllers

Navigation controllers could just set a referenced view controller as a root. Implementation of such view controller may look like this:

@interface ExternNavigationController : UINavigationController@property (strong, nonatomic) NSString *storyboardName;@property (strong, nonatomic) NSString *sceneIdentifier;@end@implementation ExternNavigationController- (void)awakeFromNib{    NSAssert(self.storyboardName, @"storyboardName is required");    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:self.storyboardName bundle:nil];    UIViewController *vc = self.sceneIdentifier        ? [storyboard instantiateViewControllerWithIdentifier:self.sceneIdentifier]        : [storyboard instantiateInitialViewController];    self.viewControllers = @[vc];}@end

View controllers

Problems begin when you want to push a view controller defined in an external storyboard. This is the case when properties are copied. Instead of this, we can implement a custom segue which will substitute a fake destination controller with a real one from external storyboard.

@interface ExternStoryboardSegue : UIStoryboardSegue@end@implementation ExternStoryboardSegue- (id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(ExternViewController *)destination{    NSAssert(destination.storyboardName, @"storyboardName is required");    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:destination.storyboardName bundle:nil];    UIViewController *vc = destination.sceneIdentifier        ? [storyboard instantiateViewControllerWithIdentifier:destination.sceneIdentifier]        : [storyboard instantiateInitialViewController];    return [super initWithIdentifier:identifier source:source destination:vc];}- (void)perform{    [[self.sourceViewController navigationController] pushViewController:self.destinationViewController animated:YES];}@end

ExternViewController is used as a placeholder and contains required for substitution properties (storyboardName and sceneIdentifier).

@interface ExternViewController : UIViewController@property (strong, nonatomic) NSString *storyboardName;@property (strong, nonatomic) NSString *sceneIdentifier;@end@implementation ExternViewController@end

We need to set these properties and custom class for placeholder view controller. And also link view controller with ExternStoryboardSegue.

IB screenshot