Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Stickerbox
Created March 27, 2018 20:48
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 Stickerbox/3fe6964edcf897483ff1221a061d5e91 to your computer and use it in GitHub Desktop.
Save Stickerbox/3fe6964edcf897483ff1221a061d5e91 to your computer and use it in GitHub Desktop.
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