Last active
June 18, 2020 09:16
-
-
Save josippetric/b8815952527e5088f01538670880358a to your computer and use it in GitHub Desktop.
Storyboarded - Protocol to create ViewControllers from a storyboard
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
| import UIKit | |
| protocol Storyboarded { | |
| static func instantiate(storyboardName: String?) -> Self | |
| } | |
| extension Storyboarded where Self: UIViewController { | |
| static func instantiate(storyboardName: String?) -> Self { | |
| // this pulls out "MyApp.MyViewController" | |
| let fullName = NSStringFromClass(self) | |
| let className = fullName.components(separatedBy: ".")[1] | |
| // load our storyboard | |
| let storyboard = UIStoryboard(name: storyboardName ?? "Main", bundle: Bundle.main) | |
| // instantiate a view controller with that identifier, and force cast as the type that was requested | |
| return storyboard.instantiateViewController(withIdentifier: className) as! Self | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment