Last active
September 19, 2017 09:08
-
-
Save andr3a88/55403bbce999d524c74c825418efad8b to your computer and use it in GitHub Desktop.
Simple storyboard initialization. Require storyboard id equal to class name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ".") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of use:
let vc = Storyboard.login.instantiate(LoginViewController.self)