Skip to content

Instantly share code, notes, and snippets.

@andr3a88
Last active September 19, 2017 09:08
Show Gist options
  • Save andr3a88/55403bbce999d524c74c825418efad8b to your computer and use it in GitHub Desktop.
Save andr3a88/55403bbce999d524c74c825418efad8b to your computer and use it in GitHub Desktop.
Simple storyboard initialization. Require storyboard id equal to class name
enum Storyboard: String {
case menu = "Menu"
case login = "Login"
case profile = "Profile"
case map = "Map"
case settings = "Settings"
public func instantiate<VC: UIViewController>(_ viewController: VC.Type) -> VC {
guard let vc = UIStoryboard(name: self.rawValue, bundle: nil).instantiateViewController(withIdentifier: VC.storyboardIdentifier) as? VC
else { fatalError("Couldn't instantiate \(VC.storyboardIdentifier) from \(self.rawValue)") }
return vc
}
}
extension UIViewController {
public static var defaultNib: String {
return self.description().components(separatedBy: ".").dropFirst().joined(separator: ".")
}
public static var storyboardIdentifier: String {
return self.description().components(separatedBy: ".").dropFirst().joined(separator: ".")
}
}
@andr3a88
Copy link
Author

andr3a88 commented Sep 19, 2017

Example of use:

let vc = Storyboard.login.instantiate(LoginViewController.self)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment