Pass data to previous viewcontroller VIPER
Asked Answered
C

1

6

I am using VIPER architecture in iOS swift. I have 2 viewcontrollers, let's say A and B. At first I'm going from A to B, performing some tasks and coming back from B to A. With MVC or MVVM, we can create protocols and transfer data from B to A. But for VIPER, i am confused. Here's my VIPER code B, while Back button is tapped:

View:

@IBAction func backButtonTapped(_ sender: UIButton) {
    presenter?.goBack()
}

Presenter:

func goBack() {
    router.back()
}

Router:

func back() {
    viewController?.navigationController?.popViewController(animated: true)
    //here I want to send data back to previous viewcontroller
}

I have tried creating one method in the Router of previous controller and sending the data through that method but it is not working as router doesn't have any instance of presenter or anything else, except view.

Countershaft answered 18/3, 2020 at 6:31 Comment(0)
O
5

Note:- In Viper Router is Unidirectional. So this might help you.

  1. Implement ProtocolDelegate in Current Module's VC
  2. Create a delegate variable in the Next Module's Router
  3. Then Simply Send Delegate Dependancy to Next Module's Router
  4. And Call your delegate method from Next Module's Router.

Module A

final class VCA: SomeProtocolDelegate {
 fund someMethod() {
      //Your task Action From Module B
    }
 }

final class ModuleARouter: WireFrameProtocol {
    fund gotoModuleB(withView vc: VCA) {
       ModuleBRouter.load(onView: vc)
    }
}

Module B

final class ModuleBRouter: WireframeProtocol {
    internal weak var delegate: SomeProtocolDelegate?
    // Here you  can add more argument on load method for delegate 
    // since in this example i'm send data back ViewController So I didn't create 
    class func load(onView VC: UIViewController) {
        //setup your VIPER protocol and class
        if let vCA = VC as? VCA {
            router.delegate = vCA
        }
    }

   func backToPreviousModule() {
      self.delegate?. someMethod()
   }
}
Ouachita answered 18/3, 2020 at 7:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.