Skip to content

Instantly share code, notes, and snippets.

@asimnajam
Created September 15, 2021 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asimnajam/0c43a9c51c07080d34c950ca4d6e1dad to your computer and use it in GitHub Desktop.
Save asimnajam/0c43a9c51c07080d34c950ca4d6e1dad to your computer and use it in GitHub Desktop.
Instantiating Storyboard Using Protocol Extension
protocol Storyboarded {
static var name: String { get }
static func instantiate() -> UIViewController
}
extension Storyboarded where Self: UIViewController {
static var name: String { return "Main" }
static func instantiate() -> UIViewController {
let storyboard = UIStoryboard(name: name, bundle: .main)
let viewController = storyboard.instantiateViewController(withIdentifier: String(describing: Self.self)
return viewController
}
}
P.S: The Upper cased Self is refer to its first static of what ever UIViewController we are extending.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment