Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active September 17, 2020 14:31
Show Gist options
  • Select an option

  • Save khanlou/1da2699e4c03827d89927de0bcd858ee to your computer and use it in GitHub Desktop.

Select an option

Save khanlou/1da2699e4c03827d89927de0bcd858ee to your computer and use it in GitHub Desktop.
import UIKit
protocol StoryboardInitializable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
static func makeFromStoryboard() -> Self
}
extension StoryboardInitializable {
static var storyboardName: String {
return String(describing: self)
}
static var storyboardIdentifier: String {
return String(describing: self)
}
static func makeFromStoryboard() -> Self {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let optionalViewController = storyboard.instantiateViewController(withIdentifier: storyboardIdentifier) as? Self
guard let viewController = optionalViewController else { fatalError() }
return viewController
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment