iOS Week: Safely typed segues and storyboards

So you use storyboards and you find yourself wanting to instantiate view controllers from one. The API to do this isn’t the nicest. First you have to create your UIStoryboard object, passing a string to define what storyboard you want. Then you pass another string in, and force unwrap to the UIViewController you want – yuck!

So instead we can create an enum and extension on UIStoryboard like this:

Each enum case is a different storyboard bundled in our app. Now we can do the exact same thing like this:

A much cleaner and safer approach.

We can also extend this to segues. The segue API is also kinda ugly, so we can create another enum and extension to clean this up for us!

Each segue that you make with an identifier, go ahead and put it in this enum as a new case. Now to perform a segue in a view controller, all we need to do is this:

The last piece of the puzzle is that pesky prepare(for segue:) method. In there you check the identifier as a string, check the destination, unwrap as the destination – yuck! And if that view controller is wrapped in a navigation controller you have to check that too and then get it out of the navigation controller. That’s a lot of boiler plate. So we can simplify it using the extension above. Now, we can just do this:

If this destination is a UINavigationController, it will automatically reach in and give you the correct view controller you want.