Skip to content

Instantly share code, notes, and snippets.

@tanjo
Created February 7, 2020 09:17
Show Gist options
  • Save tanjo/b774f49cdb9678224d294bebd81a12fa to your computer and use it in GitHub Desktop.
Save tanjo/b774f49cdb9678224d294bebd81a12fa to your computer and use it in GitHub Desktop.
Main.storyboard からインスタンス生成するための Extension
import UIKit
extension UIStoryboard {
static func mainInstantiate<T>(withViewController type: T.Type) -> T where T: UIViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let controller = storyboard.instantiateViewController(withIdentifier: type.storyboardIdentifier) as? T else {
fatalError("Could not instantiate view controller with type \(T.self)")
}
return controller
}
}
extension UIViewController {
static var storyboardIdentifier: String {
return String(describing: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment