Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- protocol StoryboardInitiable {
- static func loadFromMainStoryboard() -> Self?
- static func loadFromMainStoryboardForced() -> Self
- }
- extension StoryboardInitiable where Self : UIViewController {
- static func loadFromMainStoryboard() -> Self? {
- return UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: String(describing: self)) as? Self
- }
- static func loadFromMainStoryboardForced() -> Self {
- guard let vc = loadFromMainStoryboard() else { fatalError("Could not load \(String(describing:self)) from storyboard.") }
- return vc
- }
- }
- class AnotherViewController : UIViewController, StoryboardInitiable { }
- let newVC = AnotherViewController.loadFromMainStoryboard() // AnotherViewController?
- let newVCForced = AnotherViewController.loadFromMainStoryboardForced() // AnotherViewController
Add Comment
Please, Sign In to add comment