Created
November 7, 2022 07:02
-
-
Save rehannali/941f0c31604566179676327067150e8a to your computer and use it in GitHub Desktop.
Initialize from Storyboard
This file contains 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
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