A More Swifty way to use storyboards
Take a look at the code below:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Settings") as! SettingsViewController
Such code has 2 disadvantages:
- Storyboard name is raw string
- Controller identifier is raw string
We want to get rid of that. SwiftyStoryboard
allows to deal with both of there problems in one shot.
We create a struct that conforms to Storyboard
protocol like this one:
struct Main: StoryboardType {
enum ControllerId: String {
case settings
}
}
and now we can instantiate out controller with specific identifier like this:
let controller: SettingsViewController = Main.instantiateViewController(with: .settings)
Such approach allows us to distinguish different storyboards with different controller identifiers and give more information to compiler about our storyboards.
SwiftyStoryboard by deafult take the the type name as the name of the storyboard file. For example, in this case it will take Main.storyboard
as the name of the type is Main