Last active
August 29, 2017 19:27
-
-
Save andreaantonioni/290963251c3d0ecc843bb9ed71a15615 to your computer and use it in GitHub Desktop.
Programmatic reference to storyboards to instantiate view controllers
This file contains hidden or 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 Foundation | |
import UIKit | |
enum Storyboard: String { | |
case Main | |
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 | |
} | |
} |
This file contains hidden or 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 Foundation | |
import UIKit | |
extension UIViewController { | |
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