Last active
March 20, 2021 12:21
-
-
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
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 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