Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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