gpt4 book ai didi

ios - 创建解耦的 Router 和 ViewFactory

转载 作者:行者123 更新时间:2023-12-01 19:21:47 25 4
gpt4 key购买 nike

我正在 iOS 应用程序中尝试路由。

我目前的方法遇到了障碍,但是,我正在创建一个 ViewControllerFactory和一个 NavigationControllerRouter .

我面临的挑战是 ViewControllerFactory需要指示路由器接下来需要哪个路由以及 NavigationControllerRouter要求工厂将 View Controller 传递给导航 Controller 。

这两个组件如何在不产生强耦合的情况下进行通信?

enum Route {
case login
case home
}

protocol ViewControllerFactory {
func create(for route: Route) -> UIViewController
}

class VCFactory: ViewControllerFactory {
func create(for route: Route) -> UIViewController {
switch route {
case .login:
let viewController = UIViewController()
viewController.view.backgroundColor = .purple
return viewController
case .home:
let viewController = UIViewController()
viewController.view.backgroundColor = .red
return viewController
}
}
}

class NavigationControllerRouter {
private let navigationController: UINavigationController
private let factory: ViewControllerFactory

init(_ navigationController: UINavigationController, factory: ViewControllerFactory) {
self.navigationController = navigationController
self.factory = factory
}

func route(to route: Route) {
let viewController = factory.create(for: route)
navigationController.pushViewController(viewController, animated: true)
}
}

我的设置如下

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

private lazy var navController = UINavigationController()
private lazy var router: NavigationControllerRouter = {
let viewControllerFactory = VCFactory()
return NavigationControllerRouter(navController, factory: viewControllerFactory)
}()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: scene)
configure(window)
}

func configure(_ window: UIWindow) {
self.window = window
self.window?.makeKeyAndVisible()
self.window?.rootViewController = navController

router.route(to: .login)
}
}

我曾考虑过将路由器的弱引用传递到 View Controller 工厂中,然后将其注入(inject)到任何渲染的 View 中,但是这会耦合这两个 View ,特别是如果我决定将 View 工厂用于 subview 之类的东西,这根本不应该了解路由器。

最佳答案

我认为将引用传递给 VCFactory 没有任何问题,只要它是一个引用以确保没有保留周期。

这与 MVP 方法中使用的模式非常相似,其中 View 具有对演示者的引用,演示者也具有对 View 的引用。

您可以通过使用协议(protocol)来扩展它,以确保您的 ViewControllerFactory 不包含未使用的对路由器的引用。

protocol Routable {
var router: NavigationControllerRouter? { get }
}

...

class VCFactory: ViewControllerFactory, Routable {

weak var router: NavigationControllerRouter?
....
}

然后您应该能够传递创建时的引用

  private lazy var router: NavigationControllerRouter = {
let viewControllerFactory = VCFactory()
let router = NavigationControllerRouter(navController, factory: viewControllerFactory)

viewControllerFactory.router = router

return router
}()

关于ios - 创建解耦的 Router 和 ViewFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60527609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com