Guest User

Untitled

a guest
Jan 23rd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import UIKit
  2.  
  3. protocol StoryboardInitiable {
  4. static func loadFromMainStoryboard() -> Self?
  5. static func loadFromMainStoryboardForced() -> Self
  6. }
  7.  
  8. extension StoryboardInitiable where Self : UIViewController {
  9. static func loadFromMainStoryboard() -> Self? {
  10. return UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: String(describing: self)) as? Self
  11. }
  12.  
  13. static func loadFromMainStoryboardForced() -> Self {
  14. guard let vc = loadFromMainStoryboard() else { fatalError("Could not load \(String(describing:self)) from storyboard.") }
  15.  
  16. return vc
  17. }
  18. }
  19.  
  20. class AnotherViewController : UIViewController, StoryboardInitiable { }
  21.  
  22. let newVC = AnotherViewController.loadFromMainStoryboard() // AnotherViewController?
  23. let newVCForced = AnotherViewController.loadFromMainStoryboardForced() // AnotherViewController
Add Comment
Please, Sign In to add comment