Skip to content

Instantly share code, notes, and snippets.

@jakehawken
Last active February 14, 2018 15:54
Show Gist options
  • Select an option

  • Save jakehawken/6e354e5dd30960ec03cef2fc704309ce to your computer and use it in GitHub Desktop.

Select an option

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.
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