Last active
February 14, 2018 15:54
-
-
Save jakehawken/6e354e5dd30960ec03cef2fc704309ce to your computer and use it in GitHub Desktop.
A means of quickly giving a UIViewController class-level functionality for being instantiated as the initial VC of a Storyboard of the same name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension UIStoryboard { | |
| static func instantiateInitialVC<T: UIViewController>() -> T { | |
| let className = String(describing: T.self) | |
| return instantiateInitialVC(storyboardName: className) | |
| } | |
| static func instantiateInitialVC<T: UIViewController>(storyboardName: String) -> T { | |
| let className = String(describing: T.self) | |
| let storyboard = UIStoryboard(name: storyboardName, bundle: nil) | |
| guard let instance = storyboard.instantiateInitialViewController() as? T else { | |
| fatalError("\(className) was not properly configured with a corresponding storyboard as the initial view controller.") | |
| } | |
| return instance | |
| } | |
| } | |
| extension UIViewController { | |
| static func instantiateAsStoryboardInitialVC() -> Self { | |
| return UIStoryboard.instantiateInitialVC() | |
| } | |
| static func instantiateAsStoryboardInitialVC(storyboardName: String) -> Self { | |
| return UIStoryboard.instantiateInitialVC(storyboardName: storyboardName) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment