Swift Academy · Lesson

Coordinator Pattern for Delegate Communication

Using makeCoordinator() to relay UIKit delegate callbacks into SwiftUI.

Lesson 2 of 413 steps

Coordinator Pattern for Delegate Communication is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Why Coordinators?

UIKit delegates require an object reference. SwiftUI's value types can't be delegates. The Coordinator is the bridge.

struct ImagePicker: UIViewControllerRepresentable {
  @Binding var image: UIImage?
  // Coordinator will be the UIImagePickerControllerDelegate
}

makeCoordinator()

Implement makeCoordinator() returning a Coordinator instance. SwiftUI calls this before makeUIView/Controller.

func makeCoordinator() -> Coordinator {
  Coordinator(self)
}

The Coordinator Class

The Coordinator is typically a class (for delegate compatibility) that holds a reference to its parent representable.

class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
  var parent: ImagePicker
  init(_ parent: ImagePicker) { self.parent = parent }
}

Setting the Delegate

In makeUIViewController, assign context.coordinator as the delegate.

func makeUIViewController(context: Context) -> UIImagePickerController {
  let picker = UIImagePickerController()
  picker.delegate = context.coordinator
  return picker
}

Implementing Delegate Methods

Delegate callbacks in the Coordinator write back to SwiftUI by mutating bindings on the parent.

func imagePickerController(
  _ picker: UIImagePickerController,
  didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]
) {
  parent.image = info[.originalImage] as? UIImage
  picker.dismiss(animated: true)
}

Cancellation Callback

Implement the cancellation delegate to dismiss the picker and optionally notify SwiftUI.

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  picker.dismiss(animated: true)
}

Data Source via Coordinator

Coordinators can also implement data source protocols (e.g., UITableViewDataSource) for table/collection views.

class Coordinator: NSObject, UITableViewDataSource {
  var parent: MyTableView
  init(_ p: MyTableView) { parent = p }
  func tableView(_ tv: UITableView, numberOfRowsInSection s: Int) -> Int { parent.items.count }
  func tableView(_ tv: UITableView, cellForRowAt ip: IndexPath) -> UITableViewCell {
    let cell = tv.dequeueReusableCell(withIdentifier: "cell", for: ip)
    cell.textLabel?.text = parent.items[ip.row]
    return cell
  }
}

Keeping Coordinator Updated

When SwiftUI re-renders, updateUIViewController is called. Update the coordinator's reference to the new parent if needed.

func updateUIViewController(_ vc: UIImagePickerController, context: Context) {
  context.coordinator.parent = self
}

Multiple Delegates

A coordinator can conform to multiple delegate protocols, centralizing all UIKit callbacks.

class Coordinator: NSObject,
  UIScrollViewDelegate,
  UITextViewDelegate,
  UISearchBarDelegate { ... }

Coordinator vs @Binding

Use @Binding for simple value pass-through; use a Coordinator when UIKit requires a delegate reference.

// Simple: @Binding var text: String -> updateUIView writes uiView.text = text
// Complex: coordinator mediates UITextViewDelegate -> parent.text = textView.text

Coordinator Lifecycle

The Coordinator is created once per representable and lives as long as the representable is in the view tree.

// makeCoordinator called once → stable delegate reference
// updateUIView called many times → push state changes

Quick Check

Which method must you implement in UIViewControllerRepresentable to create the delegate bridge object?

Lesson Recap

The Coordinator pattern: implement makeCoordinator(), create a class conforming to UIKit delegate protocols, assign it via context.coordinator in makeUIViewController, and write back to SwiftUI through bindings stored in parent.

Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Coordinator Pattern for Delegate Communication” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Coordinator Pattern for Delegate Communication”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Coordinator Pattern for Delegate Communication”?

Using makeCoordinator() to relay UIKit delegate callbacks into SwiftUI. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Coordinator Pattern for Delegate Communication” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. UIViewRepresentable: Wrapping UIKit Views
  2. Coordinator Pattern for Delegate Communication
  3. UIViewControllerRepresentable
  4. UIHostingController: SwiftUI Inside UIKit
← Back to Swift Academy