ArchitectureMidMCQ

What is the Coordinator pattern and why use it?

Test your knowledge:

Explanation & Code

Answer: The Coordinator pattern moves navigation logic out of ViewControllers into dedicated Coordinator objects. Each coordinator owns a navigation flow.

Why:

  • ViewControllers shouldn't know about other ViewControllers
  • Enables reuse and deep-linking
  • Easier to test navigation flows

Code Example:

protocol Coordinator: AnyObject {
    var navigationController: UINavigationController { get }
    func start()
}

class HomeCoordinator: Coordinator {
    var navigationController: UINavigationController
    
    init(nav: UINavigationController) {
        self.navigationController = nav
    }

    func start() {
        let vc = HomeViewController()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: false)
    }

    func showDetail(for item: Item) {
        let vc = DetailViewController(item: item)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }
}

Rate your understanding:

Ready to practice more Architecture?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.