I am trying to understand the working of the Coordinator Pattern.
Here is my code:
import UIKit
import Foundation
class CheckoutCoordinator: Coordinator, ScheduleDelegate {
var childCoordinator: [Coordinator] = [Coordinator]()
var navigationController: UINavigationController
init(nav: UINavigationController) {
self.navigationController = nav
}
func start() {
let ctrl = CheckoutController.initFromStoryboard()
ctrl.coordinator = self
self.navigationController.pushViewController(ctrl, animated: true)
}
func openSchedule() {
let ctrl = ScheduleController.initFromStoryboard()
ctrl.delegate = self
self.navigationController.pushViewController(ScheduleController.initFromStoryboard(), animated: true)
}
func didSelectTimings(date: NSDate, timings: NSString, distance: Double) {
}
}
From CheckoutController, I go to ScheduleController, do some work which calls its delegate method. The delegate should update some value in CheckoutController and pop scheduleController. I am unable to find any concrete explanation of above senario and how to implement it "properly".
Note that schedule controller has no navigation forward hence no coordinator class for it.
I would not handle the delegate logic in the coordinator. Instead I would move it right into your
CheckoutController. So when calling theScheduleControllerit would look in your coordinator like this:And in your
CheckoutController, conform to theScheduleDelegatedelegate:Then in your
ScheduleControllerafter calling the delegate method, I would call the coordinator to pop the self(in that case theScheduleController).The popping logic can be solely in your viewController, but I like to keep the navigation in the Coordinator only. And in your
CheckoutCoordinator, or better in yourCoordinator(as this function is pretty general), implement the pop function.