Skip to content

Instantly share code, notes, and snippets.

@kitasuke
Created December 26, 2016 12:56
Show Gist options
  • Save kitasuke/df2d682e7c5b54a6d4477d9d91ff2a1a to your computer and use it in GitHub Desktop.
Save kitasuke/df2d682e7c5b54a6d4477d9d91ff2a1a to your computer and use it in GitHub Desktop.
import UIKit
protocol StoryboardInstantiable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
}
extension StoryboardInstantiable where Self: UIViewController {
static var storyboardName: String { return String(describing: self) }
static var storyboardIdentifier: String { return String(describing: self) }
static func instantiateFromStoryboard() -> Self {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
if let viewController = storyboard.instantiateInitialViewController() as? Self {
return viewController
}
return storyboard.instantiateViewController(withIdentifier: storyboardIdentifier) as! Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment