Skip to content

Instantly share code, notes, and snippets.

@amfathi
Last active March 20, 2021 12:21
Show Gist options
  • Save amfathi/0c0c49a008cffa0ee12b3240d2c9433b to your computer and use it in GitHub Desktop.
Save amfathi/0c0c49a008cffa0ee12b3240d2c9433b to your computer and use it in GitHub Desktop.
Utility method to load viewController from storyboard programmatically assuming that the storyboard has the same prefix as the viewController
import UIKit
extension UIViewController {
static func fromStoryboard(_ controllerIdentifier: String? = nil) -> Self? {
let name = String(describing: classForCoder())
let suffix = "ViewController"
let storyboardName = String(name.dropLast(suffix.count))
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let identifier = controllerIdentifier {
return storyboard.instantiateViewController(identifier: identifier) as? Self
} else {
return storyboard.instantiateInitialViewController() as? Self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment