How to correctly send data/call module from another module in VIPER (swift)?

461 views Asked by At

Router contains a link to navigation controller which contains stacked 1,2,3 view controllers (from 1,2,3 modules). Now module 3 commands router navigationController.popToRoot and should call someFunc() from module1.

If it is usual project I call popToRoot, get the root view controller and call someFunc() directly. But how to do it with VIPER architecture correctly?

1

There are 1 answers

0
EDUsta On

I'd suggest two ways:

1) Implement a Coordinator. Let the Coordinator know that there's a navigationController, and make it accessible by every Router, presumably bidirectional. Then, Coordinator can call Module1's method when Module3 is done.

2) Implement a Notification System. Just keep calling popToRoot in Module3's Router, and send a notification to the NotificationCenter. Since Module1 will still be alive (since they are stacked), it can act/react accordingly.

I'd vote for the first option though, having a Coordinator in VIPER immensely (and subjectively) helps with the routing.