Understanding MVVM-C - LinkedIn

Agree & Join LinkedIn

By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.

Sign in to view more content

Create your free account or sign in to continue your search

Sign in

Welcome back

Email or phone Password Show Forgot password? Sign in

or

By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.

New to LinkedIn? Join now

or

New to LinkedIn? Join now

By clicking Continue to join or sign in, you agree to LinkedIn’s User Agreement, Privacy Policy, and Cookie Policy.

Skip to main content
Understanding MVVM-C

When you start a new project or feature, you should spend some time thinking the architectural pattern to use. With a good analysis, you may avoid spending days on refactoring because of a messy codebase.

“How does an app transition from one view controller to another?”. This question is common and puzzling regarding iOS development. There are many answers, as every architecture has different implementation variations. Some do it from within the implementation of a view controller, while some use a router/coordinator, an object connecting view models.

Using the coordinator pattern in iOS apps lets us remove the job of app navigation from our view controllers, helping make them more manageable and more reusable, while also letting us adjust our app's flow whenever we need. Let's try hands on Implementing MVVM-C, Model-View-ViewModel-Coordinator

The best way to introduce Coordinator is probably to introduce the problem it aims to solve. As an iOS developer, Let's take a look at the snippet of code which uses the standard navigation

extension ListViewController: UITableViewDelegate,UITableViewDataSource { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {   ...   self.navigationController.pushViewController(detailsViewController, animated: true)  } }

We all have been doing this from the first day, and this seems simple enough. Here, The Navigation Controller is a parent of ListViewController, and yet its child commands it. UIViewController has a reference to UINavigationController in this example violates the parent-child hierarchy. So ListViewController has to responsible for managing navigation to DetailViewController, and it creates extra overhead, breaks the SOLID principles.

Another problem is that the data displayed in ViewControllers isn’t centralized. Imagine a situation where you have the following stack:

No alt text provided for this image

Do my 2nd UIViewController need to hold all data for 3rd or 4th UIViewController just to pass on? In that case, the VC2 receives and forwards data it doesn’t even need.

And what if you needed to add another VC between the VC2 and VC3 sometime in the future? The line grows longer and more complicated to debug, and for no good reason.

Coordinators

The Coordinator Pattern was first introduced to the iOS community by Soroush Khanlou (@khanlou) in his blog and during his presentation at the NSSpain conference

Coordinators are a great tool because they free our ViewController’s from a responsibility that they should not have. This helps us adhere to the  single responsibility principle, which makes our ViewController’s much leaner and easier to re-use.

Above Chaos, flow, can be easily fixed with coordinators. In short, they are objects that control one or more ViewControllers, and thus the flow of the app.

No alt text provided for this image

The  whole idea with Coordinator pattern is to keep a clear separation between layers. So ideally we want to avoid exposing a UIViewController to another, like following.

Recommended by LinkedIn

Boosting Workflow with Swift Package Manager Boosting Workflow with Swift Package Manager Sphinx Solutions Pvt. Ltd. 8 months ago Android and Jenkins: What Is the Limit? Part Two. Android and Jenkins: What Is the Limit? Part Two. Bruno Verachten 7 months ago The Ultimate Guide to Android Basics, Architecture, Components, and Development The Ultimate Guide to Android Basics, Architecture… Abdullah Abdelhakeem 7 months ago class LoginViewController: UIViewController @IBAction func navigateToDashBoard(sender: Any) {     let destinationController = DashBoardViewController()     destinationController.user = self.loggedinUser     self.navigationController.pushViewController(destinationController, animation: true)   } }

Using Coordinator Pattern, we can make sure both UIViewControllers are separated.

// handles the responsibility if LoginViewController final class LoginCoordinator: BaseCoordinator {   private let navigationcontroller: UINavigationController   public weak var delegate: LoginCoordinatorDelegate?   init(navigationcontroller:UINavigationController) {    self.navigationcontroller = navigationcontroller   }      override func start() {     if let controller = self.loginController {       self.navigationcontroller.setViewControllers([controller], animated: false)     }   }      // init login-controller   lazy var loginController: LoginViewController? = {     let viewModel = LoginViewModel()     viewModel.coordinatorDelegate = self     let controller = UIStoryboard(name: "Login", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController     controller?.viewModel = viewModel     return controller   }() }
No alt text provided for this image

And handle navigation in coordinator.

extension LoginCoordinator: LoginViewModelCoordinatorDelegate {  func loginDidSuccess(with user: User) {   self.delegate?.didFinishLoginCordinator(coordinator: self, with: user)  }  func loginFailed(error: NSError) {   self.loginController?.displayAlertMessage(error: error)  } }
No alt text provided for this image

And For Dashboard.

// handles the responsibility if Dashboard flow class DashboardCoordinator: BaseCoordinator {   private let navigationcontroller: UINavigationController   public weak var delegate: DashboardCoordinatorDelegate?   private let user: User   init(navigationcontroller:UINavigationController, with user: User) {     self.navigationcontroller = navigationcontroller     self.user = user   }      override func start() {     if let controller = self.dashboardController {       self.navigationcontroller.setViewControllers([controller], animated: false)     }   }      // init dashboard-controller with viewmodel dependency injection   lazy var dashboardController: DashboardViewController? = {     let controller = UIStoryboard(name: "Dashboard", bundle: nil).instantiateViewController(withIdentifier: "DashboardViewController") as? DashboardViewController     let viewModel = DashboardViewModel(appUser: self.user)     controller?.viewModel = viewModel     controller?.viewModel?.coordinatorDelegate = self     return controller   }() }

Handling Action from Dashboard, 'Logout' in this example.

extension DashboardCoordinator: DashboardViewModelDelegate {   func logout() {     // logout     // show login   } }

Find the GitHub Repo:

References

  • https://medium.com/sudo-by-icalia-labs/ios-architecture-mvvm-c-introduction-1-6-815204248518
  • https://khanlou.com/2015/01/the-coordinator/

Like Like Celebrate Support Love Insightful Funny Comment
  • Copy
  • LinkedIn
  • Facebook
  • X
Share

To view or add a comment, sign in

More articles by Budhabhooshan Patil

  • Replaced Myself with AI Aug 9, 2025

    Replaced Myself with AI

    After years of writing code, reviewing PRs, and collaborating with onshore-offshore teams, I've realized that in the…

    2 Comments
  • Beyond Code, Unveiling the Journey of Developers Turned Engineering Leaders Nov 18, 2023

    Beyond Code, Unveiling the Journey of Developers Turned Engineering Leaders

    Making the transition from being a skilled developer to an effective engineering manager is a journey that requires a…

    1 Comment
  • Async and Much Await ed - Swift Dec 12, 2021

    Async and Much Await ed - Swift

    Every year, at WWDC, Apple gives us a lot of new things to play around with. For the big new things, they prepare…

  • The Rise of the Mobile Applications industry and shape of Business Mar 27, 2021

    The Rise of the Mobile Applications industry and shape of Business

    90% of the time people spend on their mobile phones is spent on apps. By 2021, the number of app downloads is expected…

  • How to create your own navigation bar- iOS Jan 10, 2021

    How to create your own navigation bar- iOS

    According to Apple here, A navigation controller object manages its child view controllers using an ordered array…

    1 Comment
Show more See all articles

Others also viewed

  • The Ultimate Guide to Android Basics, Architecture, Components, and Development

    Abdullah Abdelhakeem 7mo
  • How to use Firebase Remote Config to automatically update the Scanbot SDK license key – Flutter guide

    Scanbot SDK 11mo
  • Performance issues in android

    Riyas Pullur 11mo
  • Mastering SOLID Principles in iOS Development: Best Practices for Scalable & Maintainable Code

    Dinitha Nadeesha Gamage 8mo
  • Flutter In Detail

    Orangebits Digital Solutions Private Limited 1y
  • Swift for Android: Complete Technical Deep Dive

    Ashokkumar kandasamy 1mo
  • 🚀 WorkManager in Android with Kotlin and Jetpack Compose

    Riyas Pullur 8mo
  • MVVM for iOS

    Malav S. 6y
  • What's new in Xcode 16

    Prathap Reddy 1y
  • How to integrate our Android Document Scanner SDK – a step-by-step tutorial

    Scanbot SDK 8mo
Show more Show less

Explore content categories

  • Career
  • Productivity
  • Finance
  • Soft Skills & Emotional Intelligence
  • Project Management
  • Education
  • Technology
  • Leadership
  • Ecommerce
  • User Experience
  • Recruitment & HR
  • Customer Experience
  • Real Estate
  • Marketing
  • Sales
  • Retail & Merchandising
  • Science
  • Supply Chain Management
  • Future Of Work
  • Consulting
  • Writing
  • Economics
  • Artificial Intelligence
  • Employee Experience
  • Workplace Trends
  • Fundraising
  • Networking
  • Corporate Social Responsibility
  • Negotiation
  • Communication
  • Engineering
  • Hospitality & Tourism
  • Business Strategy
  • Change Management
  • Organizational Culture
  • Design
  • Innovation
  • Event Planning
  • Training & Development
Show more Show less

Từ khóa » C Mvvm