SwiftPumpkin

2 of 22 · UI patterns

MVVM (+ Router)

MVVM with a Router: the ViewModel owns presentation state and logic, the Router owns navigation, and the view controller just binds and forwards.

How it's wired

User actionsStateCallsDataRouteView + VCLifecycle, bindingViewModelPresentation logicRouterNavigationModelDomain + services
calls or tellsreports backMVVM with a Router (MVVM-R): the view controller binds and forwards, the ViewModel decides, the Router navigates. Exact router ownership varies by codebase.
  • ViewModel: state and decisions.
  • Router: navigation.
  • View controller: binding only.
PartOwnsLooks like
Entity / ModelDomain objects, data and domain behaviorStructs, domain services
ViewModelPresentation logic, state transformations, user actions, coordination with domain services@MainActor class, no UIKit
ViewLayout, rendering and UI interactionsUIView subclasses, SwiftUI View
ViewControllerUIKit lifecycle, bindings, event forwarding and view compositionUIViewController
RouterNavigation, screen construction and transitionsHolds weak view controller

MVVM-R in Swift

One save-a-note screen. Read it top to bottom: the arrows point inward.

Contracts

The ViewModel depends on these, never on concrete types.

protocol NoteStoring: Sendable {  // crosses actors, so Sendable
  func save(_ note: ThankYouNote) async throws
}

@MainActor
protocol NoteRouting {
  func showSaved()
}

ViewModel

State and decisions. No UIKit import.

@MainActor @Observable
final class NoteViewModel {
  var recipient = ""
  var message = ""
  private(set) var error: String?
  private(set) var isSaving = false

  var canSave: Bool {
    !isSaving && NoteRules.canSave(recipient: recipient, message: message)
  }

  private let notes: any NoteStoring
  private let router: any NoteRouting

  init(notes: any NoteStoring, router: any NoteRouting) {
    self.notes = notes
    self.router = router
  }

  func save() async {
    guard !isSaving else { return }  // a double tap must not save twice
    isSaving = true
    defer { isSaving = false }
    do {
      try await notes.save(ThankYouNote(recipient: recipient, message: message))
      router.showSaved()  // navigation is the router's job
    } catch {
      self.error = "Couldn't save. Try again."
    }
  }
}

Router

Owns navigation, so screens never push each other.

@MainActor
final class NoteRouter: NoteRouting {
  weak var navigation: UINavigationController?

  func showSaved() {
    navigation?.popViewController(animated: true)
  }
}

View controller

Binds and forwards. Nothing to unit test here, by design.

final class NoteViewController: UIViewController {
  private let viewModel: NoteViewModel

  override func viewDidLoad() {
    super.viewDidLoad()
    render()
  }

  // Re-runs when any property read inside changes (iOS 17+).
  private func render() {
    withObservationTracking {
      saveButton.isEnabled = viewModel.canSave
      errorLabel.text = viewModel.error
    } onChange: { [weak self] in
      Task { @MainActor in self?.render() }
    }
  }

  @objc private func saveTapped() {
    Task { await viewModel.save() }
  }
}

Assembly and test

One place wires concrete types; the test needs no UIKit.

@MainActor
func makeNoteScreen(notes: any NoteStoring, nav: UINavigationController) -> UIViewController {
  let router = NoteRouter()
  router.navigation = nav
  return NoteViewController(viewModel: NoteViewModel(notes: notes, router: router))
}

@Test @MainActor
func savingRoutesBack() async {
  let router = RouterSpy()
  let viewModel = NoteViewModel(notes: InMemoryNotes(), router: router)
  viewModel.recipient = "Mom"
  viewModel.message = "Thanks for dinner"
  await viewModel.save()
  #expect(router.didShowSaved)
}

Initializers and outlets for the view controller are omitted.

Trade-offs, and what experts watch for

  • +Presentation logic is testable without UIKit.
  • +Navigation changes without touching screens.
  • -Every screen gets a ViewModel, even trivial ones.
  • -One more object per flow, and its lifetime to manage.
  • Massive ViewModel is the next failure mode. When a ViewModel has a second reason to change (business rules, data access, flow), pull an interactor out, or go to VIPER for that component.
  • Bindings: on iOS 17+, withObservationTracking replaces hand-rolled closures and Combine for @Observable ViewModels. On iOS 26, UIKit tracks @Observable reads automatically in updateProperties() and layoutSubviews().
  • Keep ViewModels @MainActor and their outputs value types. Async work lives in services; the ViewModel awaits and updates state.

When I'd use it

Most production UIKit featuresMVVM with a Router.
A component with several independent reasons to changeVIPER for that component.
A static screenNo ViewModel.

Expert notes

  • MVC → MVVM-R → VIPER, each step only where complexity earns it.

Check yourself

Answer out loud before you reveal. Recall is what makes it stick.

30-second answer

Explain MVVM (+ Router) in 30 seconds.

iOS codingAsked directly

MVVM vs MVC in UIKit: what exactly moves into the view model, and what does that buy you?

iOS codingAsked indirectly

The model changes from network updates while the screen is visible. How does your view model keep the view in sync?

System designAsked indirectly

Your team is debating MVC versus MVVM for a new app. What questions settle it?

System designAsked indirectly

Your team wants MVVM but refuses to adopt a reactive framework. Is that viable?

System designAsked indirectly

A year after moving to MVVM, view models are 800 lines. What went wrong?

System designAsked indirectly

Does AI-assisted development change how you'd architect an iOS app?

Read it in the app's source