Skip to content

Instantly share code, notes, and snippets.

@vdeep
Created March 3, 2018 05:32
Show Gist options
  • Save vdeep/57823610b31c3854383aeed5bff904d1 to your computer and use it in GitHub Desktop.
Save vdeep/57823610b31c3854383aeed5bff904d1 to your computer and use it in GitHub Desktop.
UIViewController extension to instantiate view controller from storyboard or xib file of the same name
import UIKit
extension UIViewController {
static func initWithStoryboard(_ storyboard: UIStoryboard? = nil,
_ identifier: String? = nil) -> UIViewController? {
var _storyboard = storyboard
if storyboard == nil {
_storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
}
guard _storyboard != nil else {
return nil
}
if let identifier = identifier {
return _storyboard?.instantiateViewController(withIdentifier: identifier)
} else {
return _storyboard?.instantiateInitialViewController()
}
}
static func initWithNib() -> Self {
return self.init(nibName: String(describing: self), bundle: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment