Guest User

Untitled

a guest
Dec 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import UIKit
  2.  
  3. protocol StoryboardIdentifiable {
  4. static var storyboardIdentifier: String { get }
  5. }
  6.  
  7. extension StoryboardIdentifiable where Self: UIViewController {
  8. static var storyboardIdentifier: String {
  9. return String(describing: self)
  10. }
  11. }
  12.  
  13. extension UIViewController: StoryboardIdentifiable {
  14. private class func instantiateViewController<T: UIViewController>(from storyBoard: UIStoryboard) -> T {
  15. guard let viewController = storyBoard.instantiateViewController(withIdentifier: storyboardIdentifier) as? T else {
  16. fatalError("\(#function) Couldn't instantiate view controller with identifier \(storyboardIdentifier) ")
  17. }
  18. return viewController
  19. }
  20.  
  21. class func initFromStoryboard() -> Self {
  22. let storyBoard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
  23. return instantiateViewController(from: storyBoard)
  24. }
  25. }
Add Comment
Please, Sign In to add comment