Skip to content

Instantly share code, notes, and snippets.

@sgr-ksmt
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgr-ksmt/30bba3f70551a5d1bf0a to your computer and use it in GitHub Desktop.
Save sgr-ksmt/30bba3f70551a5d1bf0a to your computer and use it in GitHub Desktop.
Instantiate ViewController from Storyboard
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")
}
}
@sgr-ksmt
Copy link
Author

Ex.

    let vc: HogeVC = HogeVC.instantiateFromStoryboard()

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