首页 > 解决方案 > 在 MVVM-C 中的 ViewModel 之间传递数据

问题描述

我正在使用 MVVM 和 Coordinator 来设计应用程序。我怀疑的一件事是如何在不同的ViewModels. 通常,前一个 viewModel 只会创建下一个 viewModel,并且只会在prepareforsegue. 但是,既然我负责所​​有导航,我该如何实现呢?

Class AppCoordinator : NSObject, Coordinator, UINavigationControllerDelegate {

    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    var dependencyContainer : MainDependencyContainer
    
    func start() {
        let vc = ViewController.instantiate()
        vc.coordinator = self
        vc.viewModel = dependencyContainer.makeMainViewModel()
        navigationController.delegate = self
        navigationController.pushViewController(vc, animated: true)
    }

    func createAccount() {
        let vc = CreateAccountViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }

}

我当然可以创建ViewModelfor CreateAccountViewControllerinMainViewModel并将 theViewModel作为参数传递给createAccount方法,但在这里这样做是正确的方法吗?这里的单元测试含义是什么?

标签: iosswiftmvvmdependency-injectioncoordinator-pattern

解决方案


理想情况下,您不希望两个 ViewModel 相互交互并保持两个元素分开。

处理它的一种方法是通过导航所需的最少数据。

class AppCoordinator : NSObject, Coordinator, UINavigationControllerDelegate {

    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    var dependencyContainer : MainDependencyContainer

    func start() {
        let vc = ViewController.instantiate()
        vc.coordinator = self
        let viewModel = dependencyContainer.makeMainViewModel()

        // for specific events from viewModel, define next navigation
        viewModel.performAction = { [weak self] essentialData in
            guard let strongSelf = self else { return }
            strongSelf.showAccount(essentialData)
        }

        vc.viewModel = viewModel
        navigationController.delegate = self
        navigationController.pushViewController(vc, animated: true)
    }

    // we can go further in our flow if we need to
    func showAccount(_ data: AnyObject) {
        let vc = CreateAccountViewController.instantiate()
        vc.viewModel = CreateAccountViewController(with: data)
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: true)
    }
}

更进一步,您可以创建一个特定的 Coordinator CreateAccountViewController,它将使用这些数据进行初始化。该start()方法将创建其 ViewController 所需的任何内容。

// we can go further in our flow if we need to
func showAccount(_ data: AnyObject) {
    let coordinator = CreateAccountCoordinator(data: data, navigationController: navigationController)
    coordinator.start()
    childCoordinators.append(coordinator)
}

在最后一个示例中,协调器仅负责构建其视图并在需要时将基本信息传递给下一个协调器。viewModel 只暴露给它的视图,最终视图不知道两者。在您的情况下,这可能是一个不错的选择。

最后,您可以使用协议抽象进行测试,以确保performAction触发器showAccountshowAccount创建子协调器等。


推荐阅读