Skip to content

Instantly share code, notes, and snippets.

@kruperfone
Created January 24, 2019 12:49
Show Gist options
  • Save kruperfone/083065d452f0977efee65b196d91e3d3 to your computer and use it in GitHub Desktop.
Save kruperfone/083065d452f0977efee65b196d91e3d3 to your computer and use it in GitHub Desktop.
import UIKit
extension UIViewController
{
// Storyboard identifier from class name
class var storyboardId: String {
var storyboardId = ""
let components = String(describing: self).components(separatedBy: ".")
if components.count > 0 {
storyboardId = components.last!
}
return storyboardId
}
/// Instantinate controller with storyboard identifier from class name from storyboard with passed name.
class func instantiateFromStoryboard(storyboardName: String) -> Self {
return instantiateFromStoryboardHelper(self, storyboard: UIStoryboard(name: storyboardName, bundle: nil))
}
/// Instantinate controller with storyboard identifier from class name from passed storyboard object.
class func instantiateFromStoryboard(storyboard: UIStoryboard) -> Self {
return instantiateFromStoryboardHelper(self, storyboard: storyboard)
}
/// Instantinate controller with storyboard identifier from class name from storyboard defined in Storyboard enumeration for this controller
class func instantiateFromStoryboard() -> Self {
return instantiateFromStoryboardHelper(self, storyboard: self.storyboard)
}
fileprivate class func instantiateFromStoryboardHelper<T>(_ type: T.Type, storyboard: UIStoryboard) -> T {
let controller = storyboard.instantiateViewController(withIdentifier: self.storyboardId) as! T
return controller
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment