Created
December 21, 2015 15:15
-
-
Save alexkent/b5734a50df049896d1d5 to your computer and use it in GitHub Desktop.
instantiate your UIViewController subclass from a storyboard with slightly prettier casting.
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 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage
let myVC = MyAwesomeViewController.fromStoryboard("Login")
myVC type will beMyAwesomeViewController?