Unable to send data to another module in VIPER

How can I send data from module A to module B in VIPER? I am using router A, which has information for module B, and try sending this information to view controller B or speaker B. What is the best way to do this?

+7
objective-c viper
source share
3 answers

In this case, my workflow:

  • Typically, the user interface ( view ) in module A fires an event that fires module B.
  • The event reaches the presenter in module A. presenter knows that he needs to change the module and notifies the wireframe , which knows how to make the changes.
  • wireframe in module A is notified to the wireframe in module B. In this call, all necessary data is sent
  • wireframe in module B continues normal execution by passing information to the presenter

wireframe in module A must know wireframe B

+4
source share

Using delegates to send data between VIPER modules:

 // 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 ... } } 

For more tips, check this post https://www.ckl.io/blog/best-practices-viper-architecture/

+2
source share

Does the wireframe link to the presenter? This version of VIPER that I am using

the router knows about another module and tells view to open it. The assembly combines all parts of the module.

+1
source share

All Articles