Routing in Clean Swift (VIP) Architecture by@dejanatanasov

Routing in Clean Swift (VIP) Architecture

August 1st 2018 10,870 reads
Read on Terminal Reader
react to story with heart
react to story with light
react to story with boat
react to story with money
Dejan Atanasov HackerNoon profile picture

Dejan Atanasov

Learn how to use the Router component properly


Please make sure that you have read Introducing Clean Swift (VIP) Architecture before proceeding with this tutorial_._

The Router component is responsible for passing data and handling transitions between view controllers. This is a very useful component if we utilize it properly.

For the purposes of this tutorial, I have created an example project with 2 scenes called Login and Register. There would be 2 scenarios presented below.

  1. Transitioning from Login to Register
  2. Passing data from Login to Register



I will start by showing you how to transition to the next view controller. The Router component already contains example methods that we can adapt for our needs.


We will edit the routeToSomewhere() method and name it routeToRegister() because we want to present the Register scene on a button click_._ I have commented the lines that are responsible for the passing of data which I will cover later in this tutorial.


**Routing Logic Protocol**At the top of the file, there is a protocol called RoutingLogic.This protocol will contain all the methods that are responsible for the transitioning. Everything that is declared here can be accessed from the ViewController (in our case the LoginViewController).

**routeToRegister()**CleanSwift provides us with two solutions for handling the transition between view controllers. (1) by using a segue, (2) programmatically.

#1 Using a Segue

Under LoginViewController the CleanSwift template generates for us an override of the prepare(for segue:) method. This method will be triggered each time a view controller is pushed/presented with a segue.


IMPORTANT: When you are using segues, you have to set the segue identifier to match the naming of the method.

For example, if the method of the segue identifier is set to Register then there should be a method declared under Router that should be named routeToRegister(segue: UIStoryboardSegue?).


An example Segue created from a Storyboard

#2 Programmatically

In order to navigate to the Register scene, you will need to connect an IBAction to the button and call routeToRegister(nil).

Passing Data

Passing data is also done via the routeToRegister() method. All we have to do is uncomment a few lines that were commented before due to our focus on the transition logic.

**Data Passing & Data Store Protocols**Each Router component contains a protocol that handles the data passing logic. In our case, it's called LoginDataPassing. This protocol implements a DataStore protocol that is declared under Interactor.

The DataStore protocol basically stores variables that we need to pass to the next scene. The Interactor does all the logic for populating those variables. The Router just inherits the values.

**passDataToRegister()**This method is responsible for assigning values to the destination DataStore protocol. It contains 2 attributes:

  1. Source DataSource protocol - represents the data source of the current view controller.
  2. Destination DataSource protocol - represents the data source of the destination view controller.

Assuming that we need to pass data from the Login to the Register controller, we will assign the LoginDataSource as the source, and the RegisterDataSource as the destination.

In the current example, I am passing a variable called name. In order to pass that variable, we must declare it to the both Login and Register data sources. After declaring both variables, you can assign some random string directly to the name variable in LoginInteractor (for testing purposes). Go back to the LoginRouter and inside passDataToRegister() assign the value to the destination data store_._ =

If you navigate now from Login to Register, the routeToRegister() method will be called and the RegisterDataSource protocol will contain the correct name that was assigned from the LoginDataSource protocol.

That’s it from this tutorial and if it helped you please 👏 or share this story so others like you can find it. Thank you for your attention! 🚀


Check out my latest project:

‎1x2 BET - Soccer Tips & Odds_‎HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…

Read more of my writing on Medium:

Installing the Clean Swift Template in Xcode_Clean Swift becomes more and more popular amongst iOS Developers. This short tutorial will show you how to install the…

Your ultimate guide to the Google Maps SDK on iOS, using Swift 4_Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…

SWIFT — Custom UIView with XIB file_Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…

How to add Spotlight support to your iOS app_A Swift tutorial that will make your app available in Spotlight

Core Data Relationships_Understanding One-to-One and One-To-Many

Understanding Auto Layout in Xcode 9_All you need to know about Auto

Subscribe to my Newsletter:


react to story with heart
react to story with light
react to story with boat
react to story with money
. . . comments & more!