Typed View Controllers from storyboard
psartzetakis opened this issue ยท 2 comments
Hi,๐
I have introduced something that personally annoys me, if you find that interesting and you believe that we can include a solution on that issue through PresenterKit
we can discuss it further.
The problem
When you bring view controllers from the storyboard usually you do something like that:
let colorViewController = self.storyboard?.instantiateViewController(withIdentifier: "identifier") as! ColorViewController
The thing that I don't like is that part:
as! ColorViewController
or
as? ColorViewController
A suggestion
I believe that it would be much nicer to wrap this functionality into something nicer. Perhaps something like:
extension UIStoryboard {
func instatiate<T: UIViewController>(type: T.Type, with identifier: String) -> T {
return instantiateViewController(withIdentifier: identifier) as! T
}
}
As a result we could do something like that:
let colorViewController = storyboard?.instatiate(type: ColorViewController.self, with: "identifier")
A different suggestion (not that good though..)
Note: - personally I use most of the times as identifier the same name as the class name, so it would suit me something like that:
extension UIStoryboard {
func instatiate<T: UIViewController>(type: T.Type, identifier: String? = nil) -> T {
let identifier = identifier ?? "\(type.self)"
return instantiateViewController(withIdentifier: identifier) as! T
}
}
So I can use it like that:
let colorViewController = storyboard?.instatiate(type: ColorViewController.self)
But: I don't thing that could be a good idea since it can be misleading ..
What do you think?
Thanks @psartzetakis ! ๐
Sounds cool to me, but it's probably out-of-scope for this library, which is focused on presentation.
I think this would fit better in @AliSoftware's https://github.com/AliSoftware/Reusable
He has something similar to this:
https://github.com/AliSoftware/Reusable/blob/master/Sources/Storyboard/StoryboardSceneBased.swift
But I like what you've proposed a lot. ๐ฏ
@jessesquires Thanks for the feedback! ๐