How to exchange data between View Controllers

If you have two view controller classes you probably want to exchange data between them. Thanks to two methods we can do that, provided one view controller is presented via a modal segue. Here’s how it works step by step.

Imagine you had two labels on each View Controller. We’ll call them myLabel1 and myLabel2 (1is on ViewController, 2 on MyViewController). Both View Controllers need to be connected as described previously via a segue. You can access properties on ViewController from MyViewController like so:

Likewise, if you’re in MyViewController you can access properties from ViewController like so:

About Jay Versluis

Jay is a medical miracle known as Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

2 thoughts on “How to exchange data between View Controllers

  1. Hi, access via presentedViewController doesn’t work. Only access from second View Controller via presentingViewController works. If you go back with dismissViewControllerAnimated all the properties are “null”.
    The only moment presentedViewController works well is by performSegueWithIdentifier. That’s all!

    “self.myLabel1.text = ((MyViewController *)self.presentedViewController).myLabel2.text;” doesn’t work. You’ll dismiss the viewController!

    Best regards

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.