Refactoring with Storyboard References

It has always been possible to split a large user interface into several Storyboards. The disadvantage was that you could not then create segues in Interface Builder between the different Storyboards. Instead you needed to wire up target-action connections in the Storyboard of the presenting view controller and use instantiateViewControllerWithIdentifier: to get the destination view controller to present.

Storyboard references introduced with Xcode 7 give you a way to split a large storyboard and still create the segues between scenes in Interface Builder.

Refactoring a Large Storyboard

To show how to use Storyboard references consider the following Storyboard for a simple iPhone app using a tab bar controller.

The first “news” tab contains a split view controller with the usual table view based master-detail segues. The second “settings” tab has a static table view with segues to three other view controllers.

I also have an iPad version of the Storyboard with the Settings views presented in a popover from a button on the toolbar. We can avoid duplicating those scenes in the iPad Storyboard by first refactoring them into a separate Storyboard file:

Storyboard Reference

Unfortunately the tab bar item for the Settings has lost its title and icon. I am not sure if that is a bug or a feature (a radar will follow). For now I manually add them back in the main Storyboard.

Adding a Reference to a Storyboard

To make use of the new Settings Storyboard drag a Storyboard reference from the object library into the iPad Storyboard:

Storyboard Reference

Use the attributes inspector to set the Storyboard name and identifier of the navigation controller in the Settings Storyboard:

Storyboard Reference

Once you have a Storyboard reference you can use it as the target of a segue. So for example I can create a popover segue from a bar button item to the Settings Storyboard reference:

Storyboard Reference

Room For Improvement

A disadvantage with this approach to Storyboard references is that you can no longer view the whole user interface on a single screen. I was hoping that Interface Builder would automatically resolve the references to show the contents of each of the Storyboards when looking at the main Storyboard. Maybe that will come in Xcode 8?

One other negative if you refactor a localised Storyboard is that you need to remember to manually copy the localisation strings for the new Storyboard. Xcode does not currently manage that for you.

Update 27 August 2015: I should also mention if you want to maintain backward compatibility with iOS 8 you cannot use storyboard references as the target of a relationship segue. For example, you cannot embed a storyboard reference in a tab bar controller or make it the root of a navigation controller on iOS 8.

For Further Reading

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories