Skip to content

Instantly share code, notes, and snippets.

@adithyabhat
Created August 23, 2016 17:07
This gist is to demonstrate instanceType alternative in Swift. Credits to Martin R (StackOverflow)
extension UIViewController
{
class func instantiateFromStoryboard(storyboardName: String, storyboardId: String) -> Self
{
return instantiateFromStoryboardHelper(storyboardName, storyboardId: storyboardId)
}
private class func instantiateFromStoryboardHelper<T>(storyboardName: String, storyboardId: String) -> T
{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier(storyboardId) as! T
return controller
}
}
let vc = MyViewController.instantiateFromStoryboard("name", storyboardId: "id")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment