Coordinator Pattern for Delegate Communication
Using makeCoordinator() to relay UIKit delegate callbacks into SwiftUI.
Coordinator Pattern for Delegate Communication is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.textCoordinator 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 changesQuick 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.
Frequently asked questions
Is the “Coordinator Pattern for Delegate Communication” lesson free?
Yes — the full text of “Coordinator Pattern for Delegate Communication” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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
- UIViewRepresentable: Wrapping UIKit Views
- Coordinator Pattern for Delegate Communication
- UIViewControllerRepresentable
- UIHostingController: SwiftUI Inside UIKit