Using Coordinators for Navigating between Modules in Xcode Project

Nabin Shrestha
3 min readAug 11, 2021

--

I’d written an article about writing your iOS application as modules. Here’s the link:

If you’ve done that then Coordinators is the next thing that you need to add in your project. Coordinator is the component that will handle all the routing within your application. Just like you separate the business logic of your application using various architecture like MVVM, MVP etc. Coordinators will help us separate our navigation logic inside our application.

Let’s get onto it, shall we?

When we create a new Project we have a ViewController in Main.Storyboard file set as Initial ViewController. It will be indicated by right arrow like below:

Initial View Controller

First we’ll remove this by clicking on the arrow and hitting the delete button. Now , if we run our project it will show a black screen and nothing will happen because we’ve removed the initial ViewController. We’re going to set the initial ViewController programmatically in AppDelegate.swift.

Delete the Storyboard Name property from Info.plist.

Info.plist

After deleting the property, let’s set the initial ViewController code in AppDelegate.swift. For this let’s start with creating Coordinator.

Let’s create a Coordinators Group in which we’ll have all our Coordinators. Create a Coordinator protocol like below:

The protocol consists of :

  1. childCoordinators is an array to hold the instance of created Coordinators.
  2. navigationController that will be the instance of navigationController responsible for navigation between modules.
  3. start() function will have code where we’ll initialize our ViewControllers.

Create a MainCoordinator.swift file which will be responsible for the entry point of our app.

We’ve now created a MainCoordinator class which confirms to the Coordinator protocol we’ve created just before. Let’s go back to the AppDelegate.swift and setup our code.

What we’ve done is created an optional instance of UIWindow and MainCoordinator in AppDelegate. We’ve initialized the window at first. Then we’ve created a UINavigationController instance we’ll use in our app for pushing ViewControllers. Then we’ve created instance of MainCoordinator and passed the instance of navigationController from constructor and called start() function. We set our window’s root ViewController as navigationController.

Now, we create a ViewController which is our root view controller and push it in navigationController inside our start() function.

We can run our application now and see it will be showing our ViewController with its own navigationController.

We’re done with the initial setup of our coordinators. Now let’s use this to communicate between modules. We have a Login Module where we have LoginViewController.swift. We need to push this ViewController. I’ve added a button in ViewController which we’ll use to push LoginViewController.swift.

ViewController

So, first we’ll create a protocol which we’ll use to push LoginViewController.swift and create a weak variable of that protocol in ViewController.swift.

So, when the func loginButtonTapped func is invoked, instead of pushing LoginViewController from here we’ll be invoking the delegate func login().

Let’s go back to the MainCoordinator where we’ll confirm to the delegate where to initialized our ViewController. It is shown as below:

Here we’ve confirmed to the ViewControllerCoordinatorDelegate which makes us implement the login() func. We confirm to the login() func and push LoginViewController. Also, don’t forget to import Login Module.

Similarly, we can make LoginCoordinator.swift that handles all the navigation that can be done from login page.

LoginCoordinator can handle starting the LoginViewController and starting DashboardCoordinator that pushes DashboardViewController.

The code inside the login() func now changes from above if you use LoginCoordinator.

That’s it. We can use Coordinators in our project to handle navigation in our app. Here’s a demo project if you want to checkout the implementation. Feel free to show your support by clapping the article.

--

--

No responses yet