Coordinator Swift

Tram Ho


Have you ever been in a situation where your friends become too similar and dependent on each other. And your navigation is scattered across your code.

You may also have heard of the big ViewController problem. is related to the fact that the ViewController in the MVC pattern often does too much, including the view setting code. in the way that the view controller actually becomes a view (and more …)

One of the tasks that ViewController does not perform is the management of screens navigation and application flow.

What is the coordinator?

It is an object that handles navigation flow, generally transfers Data between ViewController and has nothing to do with the business logic.

Using the coordinator pattern helps us ease the work of navgation from our viewcontroller, making them easier to manage and reuse, while allowing us to adjust the application flow at any time.

In other words: Instead of pushing and presenting your ViewControllers from other view controllers. All ViewController will be managed by the Coordinators

Therefore, the ViewController will be isolated and invisible from each other, and can be easily reused.

As illustrated in the above diagram. The Coordinator model can be described as follows:

  1. there may be one or more ViewController according to the Coordinator.
  2. Each coordinator displays its viewController (s) using a method commonly referred to as start ().
  3. Each ViewController has 1 delegate reference (delegate) to its coordinator.
  4. Each Coordinator has a series of child coordinators (ChildCoordinators).
  5. Each child coordinator has 1 delegate reference for the parent Coordinator

Demo Coordinator

Here we have 2 Coordinator and 3 ViewController to show that 1 coordinator can have 1 or more View

Create a project with 3 ViewController: FirstViewController, SecondViewController and ThirdViewController.

On each ViewController add 1 button to navigate to other ViewController

First we will create a Coordinator protocol. With 1 Coordinator array and 1 init method takes 1 direction controller (navigationController) as parameter.

FirstCoordinator manages the FirstViewController. With the method implementation start, add FirstViewController and FirstCoordinator:

FirstCoordinator has 2 extensions, 1 is used to navigate to ViewController followed by the second is used to navigate back to FirstCoordinator.

We need to update the child coordinator array with the coordinator stack

Then instead of directly creating the method in mainController. we create a FirstCoordinator with the navigationController parameter. and the start () method will be in charge of displaying the ViewController first.

For AppDelegate

For SceneDelegate

To navigate to the next ViewController. We will have to call the Coordinator represented by FirstViewControllerDelegate.

As mentioned above. One coordinator can manage one or more ViewController. So here SecondCoordinator will handle both SecondViewController and ThirdViewControllers.

First we create a custom back button that calls a coordinator coordinator method NavigateToFirstpage: Important note: when switching between coordinators. the default navigation controller’s back button will break the dispatcher’s logic if the action is not overwritten. we should absolutely call our own return action using the coordinator method to make sure the child coordinator array is updated well.

And then we add another method to pass to the ThirdViewController:

And finally, the ThirdViewController calls its delegate delegate (SecondCoordinator) to navigate back to the FirstViewController. And here there is no need to override the navigationController return button, as it will not change the current coordinator.

And this way we can navigate the loop: FirstViewController -> SecondViewController -> ThirdViewController.

Share the news now