Created
November 24, 2022 06:35
Create controllers programmatically from the Main Storyboard
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
import UIKit | |
struct Storyboard<T: UIViewController> { | |
static var storyboardName: String { | |
return String(describing: T.self) | |
} | |
static var viewController: T { | |
let storyboard = UIStoryboard(name: "Main", bundle: nil) | |
guard let vc = storyboard.instantiateViewController(withIdentifier: Self.storyboardName) as? T else { | |
fatalError("Could not get controller from Storyboard: \(Self.storyboardName)") | |
} | |
return vc | |
} | |
} | |
// Usage (Storyboard ID must match the UIViewController class name) | |
let myVC = Storyboard.viewController as MyViewController |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment