The Goal
I want to call the instantiateViewControllerWithIdentifier
method on a Storyboard to get a view controller. But I want to do it with generics for code reuse and simplicity.
The Non-Compiling Code
let viewController = StoryboardHelper.viewControllerFromStoryboard<ChooseProviderViewController>()
class func viewControllerFromStoryboard<T>() -> T {
let storyboard = mainStoryboard()
let viewController = storyboard.instantiateViewControllerWithIdentifier(resolveViewController(T)) as T
return viewController
}
private class func resolveViewController<T>() -> String {
if T is ChooseProviderViewController {
return chooseProviderViewControllerIdentifier;
}
return ""
}
Now, the above code is what I have at the moment, and how I would like the API to be. It would be nice to say "StoryboardHelper, here is the type of the view controller I want, go get the view controller instance for me".
So, in the end, I would like some way to do this:
let instanceOfXViewController = StoryboardHelper.viewControllerFromStoryboard<XViewController>()
let instanceOfYViewController = StoryboardHelper.viewControllerFromStoryboard<YViewController>()