Using Multiple Storyboards in iOS

IosUser InterfaceIos5xcode4.2Uistoryboard

Ios Problem Overview


My objective is to create a tabbed application, then the view for each of the tabs are constructed in separate storyboards.

Storyboard screenshot

My mainstoryboard is a tab view.

Then I create a secondary storyboard (storyboard#2) with 2 View Controllers. The first view controller (also ticked as initial) have a button, and segue (modal) to 2nd view. First view controller

I managed to load the view by subclassing and overriding loadView from storyboard#2. loadView override

Here's the simulator output.

Simulator

When click on the "click me" button, I get a EXC_BAD_ACCESS. The segue does not work, seems like the second storyboard is not being loaded completely.

Has anyone tried to do this before and get it working? There's a youtube video from SkillMaster.net but he does not demonstrate if a segue is working under the secondary storyboard. the video is here: http://youtu.be/D4_twoYvB4M

Thanks for any input and help!

Screenshots:

Ios Solutions


Solution 1 - 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

Solution 2 - Ios

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.

Solution 3 - Ios

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

Solution 4 - Ios

From one of my XIB files I am navigating into a more complicated part of the GUI, and for this part I am using a Storyboard. So a button in my XIB will navigate to the storyboard. The code I have for this is:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardIdentifier" bundle:nil];
UIViewController* myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];

[self.navigationController pushViewController:myStoryBoardInitialViewController animated:YES];

This will successfully push my StoryBoard onto the view. The code above is called from a buttons "Touch Up Inside" action.

Solution 5 - Ios

As from Xcode 7(and ported back to iOS8) you can now have storyboard references. It was mentioned in this WWDC 2015 session(it starts talking about them around first hour). Basically all you had to do is select ViewController's which you wish to move to separate storyboard, and click on Editor->Refactor to Storyboard.... Give it a name and voila:

enter image description here

Please note that if you had VC's which are moved to the new storyboard, and are not referenced outside of it(what it should be), you should delete their references from main.storyboard(relax they will remain in newly created storyboard, you are deleting only references to them).

Solution 6 - Ios

Apple's docs say that you may have multiple storyboards. Unfortunately they don't go into any real detail on how to do that. As you've found out, Interface Builder won't help you, so you'll have to do it in code. It works much like loading XIBs:

[UIStoryboard storyboardWithName:@”MyNewStoryboard” bundle:myBundle]

Having said that, if you don't "want one big/bloated storyboard" as you said in your comment then XIBs really are the way to go. That 'bigness' is the benefit: all the transitions between VCs are laid out in one place. Having multiple storyboards is really so that you can support multiple different and unrelated flows through your app: for example, one storyboard for a complex configuration flow and another one for the main user flow.

Solution 7 - Ios

Xcode 8.2.1

You can reference a view controller in an external storyboard.

relationship from TabBarController to Storyboard Reference

drag a connection from the UITabBarController to the external Storyboard Reference, add it as a "view controllers" relationship. In the main storyboard it shows as "Item/square", but in the external storyboard you should add a UITabBarItem and define the name and image/s for the tab.

enter image description here

Attributes inspector when the Storyboard Reference is selected.

You will also need to give the external controller a "Storyboard ID" in its storyboard (not shown here), and reference it's name in the reference.

Solution 8 - Ios

I've found that if you have a named segue in a second storyboard that you want to use in a call to performSegueWithIdenitfier: you must set the "Storyboard ID" field in the "Identity" tab on the source ViewController.

For example if you gave a VC called "ViewController1" with a segue called "segue1" to another VC, set the "Storyboard ID" of "ViewController1" to whatever (say "viewC1" or something) and then in ViewController1.m you can use [self performSegueWithIdentifier:@"segue1" sender:self].

Solution 9 - Ios

Follow below steps to achieve this task:

Step 1 : Search for an Storyboard Reference in components (Refer Screen Shot)

Step 2 : Select new storyboard reference. (Refer Screen Shot)

Step 3: Provide new storyboard name and identifier of initial view controller's identifier (Refer Screen Shot)

enter image description here

Build and Run. It will Work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaddycat Tan Yin SeeView Question on Stackoverflow
Solution 1 - IosRhubarbView Answer on Stackoverflow
Solution 2 - Iospchap10kView Answer on Stackoverflow
Solution 3 - IosvokilamView Answer on Stackoverflow
Solution 4 - IosJoachim H. SkeieView Answer on Stackoverflow
Solution 5 - Ioshris.toView Answer on Stackoverflow
Solution 6 - IosTimView Answer on Stackoverflow
Solution 7 - IosbshirleyView Answer on Stackoverflow
Solution 8 - Iosnh32rgView Answer on Stackoverflow
Solution 9 - IosDheeraj DView Answer on Stackoverflow