Untitled
unknown
plain_text
a year ago
1.3 kB
9
Indexable
Never
import SwiftUI final class Flow: UIHostingController<V> { init() { let viewModel = VM(router: self) let view = V(viewModel: viewModel) super.init(rootView: view) } @MainActor required dynamic init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension Flow: Routing { func routeToBlueBox() { navigationController?.pushViewController(BlueCoordinator(), animated: true) } func routeToRedBox() { navigationController?.present(RedCoordinator(), animated: true) } } protocol Routing: AnyObject { func routeToBlueBox() func routeToRedBox() } struct V: View { @StateObject var viewModel: VM var body: some View { VStack { Button(action: viewModel.handleActionA) { Text("Show red") }, Button(action: viewModel.handleActionA) { Text("Show blue") } } } } final class VM: ObservableObject { weak var router: Routing? init(router: Routing?) { self.router = router } func handleActionA() { router?.routeToRedBox() } func handleActionB() { router?.routeToBlueBox() } }