Browse Questions
- Classic OOP centers on classes and inheritance hierarchies — behavior is shared by subclassing a base class.
- The Coordinator pattern moves navigation logic out of ViewControllers into dedicated Coordinator objects.
- Wrap the request in a loop with a limited retry count.
- withCheckedContinuation bridges old callback-based APIs into the async/await world.
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)
}
}