Transfer data between the ViewController in UIKit (basic step by step guide)

Akshay Devkate
Nerd For Tech
Published in
3 min readApr 12, 2021
Photo by Joshua Sortino on Unsplash

There are many ways to transfer data from one view controller to another in XCode one of which is using performSegue() method to transfer the data from one view controller to other.

If you are using StoryboardUI to develop the app and want to transfer data from one view controller to other then you can use a performSegue function to transfer the data between the view controllers.

To initiate segue there must be some button, gesture, etc in the viewController Suppose we have two view controllers named A and B. We want to take the name as input in viewController A and display it in viewController B. We should first give a label to accept a name in the first view controller and a button to perform segue.

Let's create an application that accepts students information in one view controller i.e, viewController A, and displays it in the second view controller i.e, viewController B

Step 1: Create a new Xcode Project

Open Xcode -> create a new Xcode project -> choose iOS and single view app and continue -> name your project -> Give path where it should be stored and click on create.

create new ios project

Step 2: Create a new ViewController

Navigate to main.storyboard click on + in the upper right-hand corner of Xcode or press (command + shift + L) once, which will give you a drop-down menu then select UIViewController and drag it in main.stroyboard

Adding UIViewController in main.storyboard

Step3: Add Text editors and Buttons in ViewControllers

Drag and drop text labels in both view controllers using the + button in the upper right corner or pressing (Command + Shift + L) and then dragging it to the view controllers Do the same with the button.

After adding button and Text labels. right-click on the button from FirstViewcontroller in main.storyboard press control and drag the cursor to the second view controller a pop-up will appear select show This is the manual segue we created. Name this segue as per your wish in the below example view named it as FirstVC

Adding Text labels and button

Step 4: Define IBOutlet and IBAction in AViewController and BViewController

We have defined a text editor in AViewController to accept the name and another text editor in BViewController to display the name entered in AViewController. And in addition, we have also declared a button to perform the segue.

IBAction and IBOutlet

Step 5: Perform segue and transfer data between the view controllers

In AViewController when Button is tapped it will then call performSegue() function which will initiate segue way to BViewController. We have named this segue identifier as firstVC.

In override func prepare(for segue: ) function the data transfer takes place this function overrides performSegue function and transfers the data between the view controller.

For transferring data a variable is defined in BViewController and assigned to NameTwo text field where we enter the data.

--

--