I don't think prepareSegue is the perfect way to pass data between view controllers ... at least not directly.
I share your concern about using prepareForSegue
to pass values between view controllers. The source view controller should not know anything about the destination view controller (and vice versa, for that matter). Ideally , view controllers should be separate islands without being visible to each other .
To address the clutch that storyboards seem to encourage, Ive often used some form of intermediary pattern to pass data between controllers. Here is a pretty good blog post on how to implement a version of this template around the storyboard: http://coding.tabasoft.it/ios/mediator-pattern-in-swift/ . As always, this template may not be the most suitable for all situations, but I believe that it was a good solution in many of my past projects.
In principle, how a mediator template will work within the framework of the storyboard paradigm is that in each view the controllers prepareForSegue method, the segue object is passed to the mediator object. The view controller does not care about what is inside or where the navigation system is; he just knows that he is not visible. The broker that just passed the segue object (containing the source and destination view controllers) is then responsible for transferring data between the source and destination view controllers.
Using this template, each view controller is blissfully unaware of the existence of another. On the other hand, the mediator class should be aware of the relationships between the view controllers (and the view manager interfaces) in the navigation path. Obviously, if the navigation changes or the view controllers themselves change, the mediation class will need to be adjusted. Each view controller, however, should not have any dependency on each other and therefore does not need to be updated to make changes to the navigation path or changes to other view controllers along this navigation path.
source share