Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Swift protocol allowing to 1) shorten the code required to instantiate an UIViewController from a storyboard 2) store the storyboard id in the predictable place
import UIKit
/**
This protocol along with the extension provides some syntactic sugar and solves two common problems:
- Shortens the instantiation
- Answers, where to store the storyboard id
Blog post: https://kelindev.blogspot.com/2018/10/uikit-protocol-of-month.html
*/
protocol StoryboardInstantiatableViewController {
static var storyboardId: String { get }
}
extension StoryboardInstantiatableViewController where Self: UIViewController {
static func instantiate(from storyboard: UIStoryboard?) -> Self? {
return storyboard?.instantiateViewController(withIdentifier: Self.storyboardId) as? Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.