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