Created
February 7, 2020 09:17
-
-
Save tanjo/b774f49cdb9678224d294bebd81a12fa to your computer and use it in GitHub Desktop.
Main.storyboard からインスタンス生成するための Extension
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 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