Created
February 20, 2019 21:37
-
-
Save kvaDrug/e1d88092f38eb38b0c6cd91a6f7be590 to your computer and use it in GitHub Desktop.
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
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
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