Search code examples
iosdelegatesseguedelegation

delegation vs. prepareToSegue when passing data?


I'd like to know which one approach is better and why. When passing data, do you use the delegation pattern to pass data between VCs or you create the destination VC in the prepareForSegue from the VC that you are making the segue and setting the modified data directly from that vc? I'm currently using the delegation pattern, but many people I see are accessing and setting variables directly in the prepareForSegue method of the destination VC.


Solution

  • There isn't really much difference. Passing through prepareForSegue is a simpler option, but if not done correctly can create bad links in your app.

    • prepareForSegue only passes information, but delegation calls delegate methods.
    • Delegation mostly leaves everything for the delegate to do.
    • Delegate methods can be called at any time, but prepareForSegue, as the name suggests, is only called before the segue.

    Basically, they have the same effect, but take different paths to the effect.