Skip to content

Instantly share code, notes, and snippets.

@alexkent
Created December 21, 2015 15:15
Show Gist options
  • Save alexkent/b5734a50df049896d1d5 to your computer and use it in GitHub Desktop.
Save alexkent/b5734a50df049896d1d5 to your computer and use it in GitHub Desktop.
instantiate your UIViewController subclass from a storyboard with slightly prettier casting.
import Foundation
extension UIViewController {
class func fromStoryboard(name: String, identifier: String? = .None, bundle: NSBundle? = .None) -> Self? {
return fromStoryboardHelper(self, name: name, identifier: identifier, bundle: bundle)
}
private class func fromStoryboardHelper<T: UIViewController>(type: T.Type, name: String, identifier: String?, bundle: NSBundle?) -> T? {
if let identifier = identifier {
return UIStoryboard(name: name, bundle: bundle).instantiateViewControllerWithIdentifier(identifier) as? T
} else {
return UIStoryboard(name: name, bundle: bundle).instantiateInitialViewController() as? T
}
}
}
@alexkent
Copy link
Author

example usage let myVC = MyAwesomeViewController.fromStoryboard("Login") myVC type will be MyAwesomeViewController?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment