12

如何在 VIPER 中将数据从模块 A 发送到模块 B?我使用路由器 A,其中包含模块 B 的信息,并尝试将此信息发送到视图控制器 B 或演示者 B。最好的方法是什么?

4

3 回答 3

6

在这种情况下,我的工作流程是:

  1. 通常,模块 A 中的用户界面 ( view) 会启动一个触发模块 B 的事件。
  2. 事件到达presenter模块 A。它presenter知道它必须更改模块并通知wireframe谁知道如何进行更改。
  3. wireframe模块 A 通知wireframe模块 B。在此调用中发送所需的所有数据
  4. wireframein 模块 B 继续正常执行,将信息传输到presenter

wireframe在模块 A 中必须知道wireframeB

于 2016-07-27T07:25:12.457 回答
5

使用委托在 VIPER 模块之间发送数据:

// 1. Declare which messages can be sent to the delegate

// ProductScreenDelegate.swift
protocol ProductScreenDelegate {
//Add arguments if you need to send some information
    func onProductScreenDismissed()
    func onProductSelected(_ product: Product?)
}

// 2. Call the delegate when you need to send him a message

// ProductPresenter.swift
class ProductPresenter {

    // MARK: Properties
    weak var view: ProductView?
    var router: ProductWireframe?
    var interactor: ProductUseCase?
    var delegate: ProductScreenDelegate?
}

extension ProductPresenter: ProductPresentation {

    //View tells Presenter that view disappeared
    func onViewDidDisappear() {

        //Presenter tells its delegate that the screen was dismissed
        delegate?.onProductScreenDismissed()
    }
}

// 3. Implement the delegate protocol to do something when you receive the message

// ScannerPresenter.swift
class ScannerPresenter: ProductScreenDelegate {

    //Presenter receives the message from the sender
    func onProductScreenDismissed() {

        //Presenter tells view what to do once product screen was dismissed
        view?.startScanning()
    }
    ...
}

// 4. Link the delegate from the Product presenter in order to proper initialize it

// File ScannerRouter.swift
class ProductRouter {

    static func setupModule(delegate: ProductScreenDelegate?) -> ProductViewController {
        ...
        let presenter = ScannerPresenter()

        presenter.view = view
        presenter.interactor = interactor
        presenter.router = router
        presenter.delegate = delegate // Add this line to link the delegate
        ...
        }
}

有关更多提示,请查看此帖子https://www.ckl.io/blog/best-practices-viper-architecture/

于 2017-04-11T02:00:58.883 回答
3

线框是否参考了演示者? 我使用的这个版本的 VIPER

路由器知道另一个模块并告诉视图打开它。组装结合了模块的所有部分。

于 2016-07-28T06:55:18.380 回答