Skip to content

Instantly share code, notes, and snippets.

@teddichiiwa
Created April 16, 2019 06:52
Show Gist options
  • Save teddichiiwa/8da9baabe041f8e35f284435b4cee14e to your computer and use it in GitHub Desktop.
Save teddichiiwa/8da9baabe041f8e35f284435b4cee14e to your computer and use it in GitHub Desktop.
Coordinator Pattern with Storyboards
import UIKit
protocol StoryboardInstantiable: NSObjectProtocol {
associatedtype MyType
static var defaultFileName: String { get }
static func instantiateViewController(_ bundle: Bundle?) -> MyType
}
extension StoryboardInstantiable where Self: UIViewController {
static var defaultFileName: String {
return NSStringFromClass(Self.self).components(separatedBy: ".").last!
}
static func instantiateViewController(_ bundle: Bundle? = nil) -> Self {
let fileName = defaultFileName
let sb = UIStoryboard(name: fileName, bundle: bundle)
return sb.instantiateInitialViewController() as! Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment