Files
2020-12-02 23:17:06 +03:00

65 lines
2.2 KiB
Swift

//
// JSViewController.swift
// JSNavigationController
//
import AppKit
class JSViewController: NSViewController, JSNavigationBarViewControllerProvider {
private struct Constants {
static let segueTemplatesKey = "segueTemplates"
static let segueIdentifierKey = "identifier"
static let navigationControllerPushIdentifier = "navigationControllerPush"
static let navigationBarViewControllerIdentifier = "navigationBarViewController"
}
private(set) var destinationViewController: NSViewController?
private(set) var destinationViewControllers: [String: NSViewController] = [:]
var navigationBarVC: NSViewController?
weak var navigationController: JSNavigationController?
func navigationBarViewController() -> NSViewController {
guard let navigationBarVC = self.navigationBarVC else { fatalError("You must set the navigationBar view controller") }
return navigationBarVC
}
// MARK: - View Lifecycle
override func awakeFromNib() {
if type(of: self).instancesRespond(to: #selector(NSViewController.awakeFromNib)) {
super.awakeFromNib()
}
self.setupSegues()
}
// MARK: - Segues
private func setupSegues() {
guard let segues = self.value(forKey: Constants.segueTemplatesKey) as? [NSObject] else { return }
for segue in segues {
if let id = segue.value(forKey: Constants.segueIdentifierKey) as? String {
self.performSegue(withIdentifier: id, sender: self)
}
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
guard let segueIdentifier = segue.identifier else { return }
switch segueIdentifier {
case Constants.navigationBarViewControllerIdentifier:
self.navigationBarVC = segue.destinationController as? NSViewController
default:
if segueIdentifier.contains(Constants.navigationControllerPushIdentifier) {
if segueIdentifier.count > Constants.navigationControllerPushIdentifier.count && segueIdentifier.contains("#") {
if let key = segueIdentifier.split(separator: "#").map({ String($0) }).last {
self.destinationViewControllers[key] = segue.destinationController as? NSViewController
}
} else {
self.destinationViewController = segue.destinationController as? NSViewController
}
}
}
}
}