Skip to content

Instantly share code, notes, and snippets.

@chriswill0w
Forked from Gurdeep0602/AppStoryboard.swift
Created July 26, 2018 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriswill0w/3654bce04f9425c39f44d9dc122a5269 to your computer and use it in GitHub Desktop.
Save chriswill0w/3654bce04f9425c39f44d9dc122a5269 to your computer and use it in GitHub Desktop.
AppStoryboard enumeration
//
// AppStoryboards.swift
// AppStoryboards
//
// Created by Gurdeep on 15/12/16.
// Copyright © 2016 Gurdeep. All rights reserved.
//
import Foundation
import UIKit
enum AppStoryboard : String {
case Main
var instance : UIStoryboard {
return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
}
func viewController<T : UIViewController>(viewControllerClass : T.Type, function : String = #function, line : Int = #line, file : String = #file) -> T {
let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID
guard let scene = instance.instantiateViewController(withIdentifier: storyboardID) as? T else {
fatalError("ViewController with identifier \(storyboardID), not found in \(self.rawValue) Storyboard.\nFile : \(file) \nLine Number : \(line) \nFunction : \(function)")
}
return scene
}
func initialViewController() -> UIViewController? {
return instance.instantiateInitialViewController()
}
}
extension UIViewController {
// Not using static as it wont be possible to override to provide custom storyboardID then
class var storyboardID : String {
return "\(self)"
}
static func instantiate(fromAppStoryboard appStoryboard: AppStoryboard) -> Self {
return appStoryboard.viewController(viewControllerClass: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment