Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for instantiate viewcontroller using identifier #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@ import UIKit
protocol StoryboardInstantiable: NSObjectProtocol {
associatedtype T
static var defaultFileName: String { get }
static func instantiateViewController(_ bundle: Bundle?) -> T
static var defaultIdentifier: String { get }
static func instantiateViewController(_ bundle: Bundle?, _ identifier: String?) -> T
}

extension StoryboardInstantiable where Self: UIViewController {
static var defaultFileName: String {
return NSStringFromClass(Self.self).components(separatedBy: ".").last!
}

static func instantiateViewController(_ bundle: Bundle? = nil) -> Self {
static var defaultIdentifier: String {
return NSStringFromClass(Self.self).components(separatedBy: ".").last! + "Identifier"
}

static func instantiateViewController(_ bundle: Bundle? = nil, _ identifier: String? = nil) -> Self {
let fileName = defaultFileName
let storyboard = UIStoryboard(name: fileName, bundle: bundle)
guard let vc = storyboard.instantiateInitialViewController() as? Self else {

if let storyboardIdentifier = identifier {
guard let viewController = storyboard.instantiateViewController(withIdentifier: storyboardIdentifier) as? Self else {
fatalError("Cannot instantiate view controller \(Self.self) from storyboard with name \(fileName) using identifier \(storyboardIdentifier)")
}
return viewController
}

guard let initialViewController = storyboard.instantiateInitialViewController() as? Self else {

fatalError("Cannot instantiate initial view controller \(Self.self) from storyboard with name \(fileName)")
}
return vc
return initialViewController
}
}