无法将数据发送到VIPER中的其他模块

时间:2016-07-18 08:04:32

标签: objective-c viper

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

3 个答案:

答案 0 :(得分:6)

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

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

    wireframe必须知道wireframe B

答案 1 :(得分:4)

使用委托在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/

答案 2 :(得分:1)

线框 是否引用了演示者? This version of VIPER which i use

路由器 了解另一个模块,并告诉 视图 将其打开。 程序集 结合了模块的所有部分。