Learn how to use the Router component properly https://clean-swift.com/clean-swift-ios-architecture/ Please make sure that you have read before proceeding with this tutorial_._ Introducing Clean Swift (VIP) Architecture The component is responsible for passing data and handling transitions between view controllers. This is a very useful component if we utilize it properly. Router 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. Transitioning from Login to Register Passing data from Login to Register Transitioning 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. LoginRouter We will edit the method and name it 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. routeToSomewhere() routeToRegister() **Routing Logic Protocol**At the top of the file, there is a called 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). protocol RoutingLogic. **routeToRegister()** provides us with two solutions for handling the transition between view controllers. (1) (2) . CleanSwift by using a segue, programmatically #1 Using a Segue Under LoginViewController the CleanSwift template generates for us an override of the method. This method will be triggered each time a view controller is pushed/presented with a segue. prepare(for 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 then there should be a method declared under Router that should be named . Register 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 method. All we have to do is uncomment a few lines that were commented before due to our focus on the transition logic. routeToRegister() **Data Passing & Data Store Protocols**Each Router component contains a protocol that handles the data passing logic. In our case, it's called . This protocol implements a that is declared under Interactor. LoginDataPassing DataStore protocol The 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. DataStore protocol **passDataToRegister()**This method is responsible for assigning values to the destination DataStore protocol. It contains 2 attributes: - represents the data source of the current view controller. Source DataSource protocol 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 , and the RegisterDataSource as the source destination. In the current example, I am passing a variable called . 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 variable in LoginInteractor (for testing purposes). and inside assign the value to the destination data store_._ name name Go back to the LoginRouter passDataToRegister() destination.name = source.name If you navigate now from Login to Register, the method will be called and the will contain the correct name that was assigned from the . routeToRegister() RegisterDataSource protocol 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: _HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…_apple.co 1x2 BET - Soccer Tips & Odds Read more of my writing on Medium: _Clean Swift becomes more and more popular amongst iOS Developers. This short tutorial will show you how to install the…_medium.com Installing the Clean Swift Template in Xcode _Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…_medium.freecodecamp.org Your ultimate guide to the Google Maps SDK on iOS, using Swift 4 _Custom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…_medium.com SWIFT — Custom UIView with XIB file _A Swift tutorial that will make your app available in Spotlight search_hackernoon.com How to add Spotlight support to your iOS app _Understanding One-to-One and One-To-Many relationships_hackernoon.com Core Data Relationships _All you need to know about Auto Layout_hackernoon.com Understanding Auto Layout in Xcode 9 Subscribe to my Newsletter:
Share Your Thoughts