Let’s say you want to present a modal screen in your app. It’s probably just a simple table view. And of course you need some way to get back out of this screen, so you stick the table view controller inside a navigation controller, and then present that navigation controller instead.
All very straightforward so far.
But something about all this starts to irk me:
- It’s quite repetitive work
- My storyboard starts to become a little littered with navigation controllers that sit that doing very little, particularly if there’s no customisation of the nav bar to be performed
- But most of all, I don’t like writing code like this:
Something about having to fish into the navigation controller’s view controllers array feels a bit fragile to me. You lose some type-checking, and it seems to break a little of the abstraction that storyboards promise.
Instead, I’ve been toying with a setup like this:
- Set up your view controller in the storyboard as usual, but don’t include a navigation controller this time
- You probably want to change the Simulated Metrics to include a translucent nav bar, so the navigation item can be conveniently configured
- Hook up a “Custom” segue to this view controller, specifying the class name from the code above
When the segue is triggered at runtime, it’ll package up the destination into a navigation controller and nicely present that. Now both my storyboard, and the segue code are cleaner:
If needed, you can get hold of the raw navigation controller from the segue, and make adjustments to it. Oh, and this setup also makes a lot easier to switch between push and modal navigations while still designing your app’s flow.