Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import UIKit
enum Storyboard: String {
case main
var value: String {
return self.rawValue.capitalized
}
}
extension UIStoryboard {
convenience init(_ name: Storyboard) {
self.init(name: name.value, bundle: nil)
}
func instantiateViewController<T: UIViewController>() -> T {
if let name = NSStringFromClass(T.self).components(separatedBy: ".").last, let vc = instantiateViewController(withIdentifier: name) as? T {
return vc
}
fatalError("Could not find " + String(describing: T.self))
}
}
enum Segue: String {
case showLogin
}
extension UIViewController {
func perform(segue: Segue) {
performSegue(withIdentifier: segue.rawValue, sender: nil)
}
}
extension UIStoryboardSegue {
func destination<T: UIViewController>(matching segue: Segue, as vc: T.Type) -> T? {
if identifier != segue.rawValue { return nil }
if let nav = destination as? UINavigationController {
return nav.viewControllers.first as? T
}
return destination as? T
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment