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.
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! }
Open your storyboard where your different viewController are located. Tap the viewController you would like your navigation controller to start from.
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.
Note:- In Viper Router is Unidirectional. So this might help you.
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With