Created
September 15, 2021 10:18
-
-
Save asimnajam/0c43a9c51c07080d34c950ca4d6e1dad to your computer and use it in GitHub Desktop.
Instantiating Storyboard Using Protocol Extension
This file contains 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
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