Skip to content

Instantly share code, notes, and snippets.

@rehannali
Created November 7, 2022 07:02
Show Gist options
  • Save rehannali/941f0c31604566179676327067150e8a to your computer and use it in GitHub Desktop.
Save rehannali/941f0c31604566179676327067150e8a to your computer and use it in GitHub Desktop.
Initialize from Storyboard
public extension UIViewController {
class func initializeFromStoryboard(storyboardName: CONSTANTS.StoryboardName, bundle: Bundle? = nil, identifier: String = "") -> Self {
return initializeFromStoryboard(storyboardName: storyboardName.rawValue, bundle: bundle, identifier: identifier)
}
class func initializeFromStoryboard(storyboardName: String, bundle: Bundle? = nil, identifier: String = "") -> Self {
let storyboard = UIStoryboard(name: storyboardName, bundle: bundle)
if let controller = initializeFromStoryboard(storyboard: storyboard, type: self, identifier: identifier) {
return controller
}
OLLogger.shared.error("Unable to initialize controller from storyboard. Check storyboard or controller identifier ---- \(String(describing: type(of: self)))")
fatalError("Unable to initialize controller from storyboard. Check storyboard or controller identifier")
}
class func initializeFromStoryboard<T: UIViewController>(storyboard: UIStoryboard, type: T.Type, identifier: String) -> T? {
if let controller = storyboard.instantiateViewController(withIdentifier: identifier == "" ? String(describing: self) : identifier) as? T {
return controller
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment