Based on Thomas Kamin's answer, here is an extension of the UIViewController in Swift 3.
extension UIViewController { class func fromStoryboard(_ name: String, in bundle: Bundle? = nil, withIdentifier id: String? = nil) -> Self? { return fromStoryboard(UIStoryboard(name: name, bundle: bundle), withIdentifier: id) } class func fromStoryboard(_ storyboard: UIStoryboard, withIdentifier id: String? = nil) -> Self? { return fromStoryboard(storyboard, withIdentifier: id, as: self) } private class func fromStoryboard<T>(_ storyboard: UIStoryboard, withIdentifier id: String? = nil, as type: T.Type) -> T? { return storyboard.instantiateViewController(withIdentifier: id ?? "\(type)") as? T } }
If your storyboard controller identifiers match their class names, just call the from(storyboard:) class function with the name.
let viewController = MyCustomViewController.fromStoryboard("Main")
Otherwise, specify the identifier.
let viewController = MyCustomViewController.fromStoryboard("Main", withIdentifier: "ID")
If you already have an instance of the storyboard, use can use it.
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) let viewController = MyCustomViewController.fromStoryboard(storyboard, withIdentifier: "ID")
source share