I have a flow (Flow A
) of ViewController's in my iOS app. Flow A is rather complex (depending on circumstances, some view controllers are shown early, or not shown at all, etc.). In order to handle that I am using the coordinator pattern.
The code (simplified):
protocol Coordinator {
func start()
}
protocol FlowACoordinatable {
var coordinator: FlowACoordinator
}
class FlowACoordinator: Coordinator {
private var navigationController: UINavigationController
private var firstVC: FirstViewController
private var secondVC: SecondViewController
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func start() { ... }
func present(_ viewController: (FlowACoordinatable & UIViewController)) {
viewController.coordinator = self
self.navigationController.pushViewController(viewController, animated: true)
}
...
}
class FirstViewController: UIViewController, FlowACoordinatable {
var coordinator: FlowACoordinator?
func buttonTapped() {
self.coordinator?.goToNextStep()
}
}
....
FlowACoordinator
contains logic about how and when to present view controller's using the present()
method. So far so good.
Now I have a second flow, Flow B
, mostly different from Flow A. Except I want to share a view controller between the two, let's call it SharedViewController
. This is where things get strange, because I have no really good idea on how to share this view controller between the two.
The problem: I have a two way communication - the coordinator sets itself as the coordinator of the view controller it presents & the view controller calls methods on the coordinator as a response to user interaction. SharedViewController
is managed by one of two Coordinators, though, and somehow it has to pass information to the current Coordinator, regardless of which one it is.
So far I found two solutions, both of them not satisfying:
-
An additional coordinator that handles just
SharedViewController
- this is a lot of overhead and largely defeats the purpose of Coordinators. -
Implementing
FlowACoordinatable
,FlowBCoordinatable
, ... inSharedViewController
, and have multiple coordinator properties and call all of them at appropriate times. Also a lot of overhead, boilerplate code and calls to coordinators.
Any ideas on how to solve this nicely?
Aucun commentaire:
Enregistrer un commentaire