Last active
March 6, 2019 09:20
-
-
Save NeilsUltimateLab/25f0bea633b1e5c3a7d7be7031a00e6a to your computer and use it in GitHub Desktop.
Instantiate view controller from Storyboard enum.
This file contains hidden or 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 Identifiable { | |
static var identifier: String { get } | |
} | |
extension Identifiable where Self: UIViewController { | |
static var identifier: String { | |
return String(describing: self) | |
} | |
} | |
extension UIViewController: Identifiable {} | |
enum Storyboard: String { | |
case main = "Main" | |
case login = "Login" | |
case settings = "Settings" | |
} | |
extension UIViewController { | |
static func instantiate<T: UIViewController>(from storyboard: Storyboard) -> T { | |
let storyboard = UIStoryboard(name: storyboard.rawValue, bundle: nil) | |
guard let viewController = storyboard.instantiateViewController(withIdentifier: self.identifier) as? T else { | |
fatalError("Storyboard ID is not same as \(self.identifier)") | |
} | |
return viewController | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment