Different Ways to Pass Data Between ViewControllers

Different ways to How to pass data between ViewControllers

One of the most problems that iOS developers face is the passing of data between multiple ViewControllers, so in this post we are going to learn how to pass data between the view controllers so that data from one view controller can be used in the other view controller

So now you have a data in 1st View controller and you want to pass that data in the next view controller and you don’t know how to pass it, then you are at the right place, today here we are going yo discuss different ways in which you can pass data between the View Controllers.

Following things are needed:

1st View Controller – VC1

2nd View Controller – VC2

1st way to pass the data: Using Segue

  • All you have to do in this method is draw a segue from VC1 to the VC2 and name it as “toVC2” from the property inspector of the Segue
  • Now come to the VC1 and and create a Button which will send the data to VC2 on clicking, name the button as “send” and a label in which some data that is stored in variable VC1data will be showed, here VC1data variable will have String “Monday is Boring”  and on running it should show following result on the simulator
  • Now go to the VC2 and drag a label and create a variable “recievedData” in which the data that will be received will be stored for showing in the label
  • Now all we need to do is to pass the data using the Segue
  • For doing that we need to just write the following Code in the Action of the button”send”

Different ViewController


self.performSegue (withIdentifier: “toVC2”, sender: self)

}        without{} braces

  • Once this is done on clicking of the Send button you will see the VC2 but the data wont be sent
  • For sending the data all you have to do is write a function

         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == “toVC2”


            let vc = segue.destination as? VC2

            vc.recievedData = VC1data




  • And run the simulator, so when You will click send button , the String “Monday is Boring” will be assigned to the recievedData with the help of “vc” object and the label on VC2 will show the String “Monday is Boring”

We will learn more about this in next blogs

Thank you


Article Written and Submitted By: Niket

Leave a Reply

Your email address will not be published. Required fields are marked *