Skip to content

Instantly share code, notes, and snippets.

@andreaantonioni
Last active August 29, 2017 19:27
Show Gist options
  • Save andreaantonioni/290963251c3d0ecc843bb9ed71a15615 to your computer and use it in GitHub Desktop.
Save andreaantonioni/290963251c3d0ecc843bb9ed71a15615 to your computer and use it in GitHub Desktop.
Programmatic reference to storyboards to instantiate view controllers
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
}
}
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