Last active
August 29, 2015 14:27
-
-
Save sgr-ksmt/30bba3f70551a5d1bf0a to your computer and use it in GitHub Desktop.
Instantiate ViewController from Storyboard
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 UIKit | |
extension UIViewController { | |
class func instantiateFromStoryboard<T: UIViewController>() -> T { | |
if let name = NSStringFromClass(T.self)?.componentsSeparatedByString(".").last { | |
let storyboard: UIStoryboard = UIStoryboard(name: name, bundle: nil) | |
if let vc = storyboard.instantiateInitialViewController() as? T { | |
return vc | |
} | |
fatalError("failed to load \(name) ViewController From Storyboard") | |
} | |
fatalError("failed to load ViewController From Storyboard") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ex.