从历史上看,UIKit
我们一直使用协调器模式来处理导航。
启动一个新的应用程序,SwiftUI
但是不清楚如何处理这个问题。
例如,我们当前的流程是:
- 应用程序启动
- 应用协调器启动
- 加载“开始场景”
- 启动场景检查身份验证状态并在 App Coordinator 上调用委托
- App Coordinator 启动
showHomeScene
或showAuthScene
就像是
final class AppCoordinator: BaseCoordinator {
private(set) var navigationController: UINavigationController?
init(navigationController: UINavigationController?) {
self.navigationController = navigationController
}
override func start() {
showStartScene()
}
private func showStartScene() {
let configurator = StartConfigurator()
let viewController = configurator.create(self)
navigationController?.setViewControllers([viewController], animated: false)
}
private func showHomeScene() {
let coordinator = HomeCoordinator(self, navigationController: navigationController)
store(coordinator: coordinator)
coordinator.start()
}
private func showAuthScene() {
let coordinator = AuthCoordinator(self, navigationController: navigationController)
store(coordinator: coordinator)
coordinator.start()
}
}
extension AppCoordinator: StartSceneDelegate {
func userNeedsToAuthenticate() {
showAuthScene()
}
func userIsAuthenticated() {
showHomeScene()
}
}
但是,由于我们不使用UIViewController
如何navigationController?.setViewControllers([viewController], animated: false)
工作?
我们还应该使用 a 设置UIHostingController
吗?
就像是 -
navigationController?.setViewControllers([UIHostingController(rootView: StartView())], animated: false)
这似乎有点奇怪,因为我不相信 SwiftUI 视图会真正使用UINavigationController
aNavigationView
和NavigationLink
.
除非UINavigationController
真的只是作为一个包装?
我正在考虑@EnvironmentObject
在协调器中使用并基于身份验证状态替换协调器中的根视图控制器。