Do you want to be a proficient iOS developer? Then you need to understand the ideas behind view controller communication.

Nowadays, most of the applications requires a data communication between two viewcontrollers. Well, there are too many ways to do the same. But, we are going to discuss some of them. Which are efficient and easy to develop.

Firstly, there are only two directions in which data can flow.

• forward, to any new view controller that comes on the screen; and

• backwards, to a view controller that was previously on display and to which the user goes back at some point.

Passing Data Between View Controllers Using Segues (A → B)

Passing data forward is used when you want to show some information in a detail view controller. For example, view controller A might contain a list of teams that the user can select, and view controller B might show some detailed information on a single team that the user selected. In this case, you would create a property on B like this:

class ViewControllerB: UIViewController {

var selectedTeam: String = "noname"


Then, to pass the data from MainViewController to secondaryViewController you use a special function called prepare(for:sender:). This method is invoked before the segue, so you can customize it.

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

if segue.identifier == "showDetail" {

if let indexPath = self.tableView.indexPathForSelectedRow {

let controller = segue.destination as! ViewControllerB

controller.selectedTeam= objects[indexPath.row]




Passing Data Between View Controllers With Properties

Let’s say this view controller is part of a navigation controller and we want to pass some data to a second view controller.

How you set that property depends on how are you showing the detail view controller. For example, if you're using a UINavigationController and want to push the new view controller onto the stack, you would write this:

let viewControllerB = ViewControllerB()

viewControllerB.selectedTeam= "india"

navigationController?.pushViewController(viewControllerB, animated: true)

Passing Data Back With Delegation

To pass data back, the most common approach is to create a delegate property in your detail view controller, like this:

With delegation, a base class can hand off functionality to a secondary class. A coder can then implement this secondary class and respond to events from the base class, by making use of a protocol. It’s decoupled! Delegation is an important and frequently used software design pattern in the iOS SDK. It’s critical to understand if you’re coding iOS apps!

class ViewControllerB: UIViewController {

var selectedName: String = "Anonymous"

weak var delegate: ViewControllerA!


When creating your detail view controller, make sure you set up its delegate property, like this:

let viewControllerB = ViewControllerB()

viewControllerB.selectedTeam= "india

viewControllerB.delegate = self

navigationController?.pushViewController(viewControllerB, animated: true)

With this set up complete, you can now create a method in your master view controller that should be called by the detail view controller. For example, you might have something like this:

func updatedSelectedName(newName: String) {

// do something with newName