Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data to previous viewcontroller VIPER

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.

like image 427
Arnab Avatar asked Mar 18 '20 06:03

Arnab


People also ask

How do you pass data backwards in Swift?

To pass data back, the most common approach is to create a delegate property in your detail view controller, like this: class ViewControllerB: UIViewController { var selectedName: String = "Anonymous" weak var delegate: ViewControllerA! }

How do I revert to a previous view controller in Swift?

Open your storyboard where your different viewController are located. Tap the viewController you would like your navigation controller to start from.

How can you reload a viewController after dismissing a modally presented view controller in Swift?

After you have set your delegate, just add a call to the delegate method right before calling the dismissViewControllerAnimated in your Table2VC instance. This will give you precise control over when the table will get reloaded.


1 Answers

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()
   }
}
like image 119
PRAVEEN Avatar answered Oct 19 '22 18:10

PRAVEEN