4 ways to make your two view controllers to communicate with themselves

Some years ago before I became a Jr. iOS developer, some company asked me in an interview the following question: "If you have a view controller that presents another, how can I make this second view controller to update some data within the previous one?". I spent some minutes to achieve my answer and I didn't know how to explain that, because for me only the first screen could send some data to the other and it would no more belong to the next screen's context. Fortunately, 2 years passed since that interview and now I can teach you 4 simple ways to make not only two view controllers, but any components in your app to update data between them. Hope you enjoy!

1)Delegation

It is the most famous way to make a class to tell some important event to another. Certainly you have dealt with delegates when implementing UITableViews, UICollectionViews or many other UI components in Swift. Let's formalize what exactly delegation consists.

Delegation is defined as a mechanism where a class holds an object that implements a protocol, which declares a list of events that happen in your first class. If you have some kind of button, or selectable component, you can define a delegation protocol that implements some events for that button like didTap or didScroll or any kinda interaction the user shall make. The button itself must hold an object that implements its delegation protocol and when those events happen in that class, you only have to call the event methods of the delegate. Take a look on this class:

Repair that we have already some UIResponder events in the button itself, but this specific class has a delegate object which is told every time one of those events is triggered.

The same works with two view controllers:

Well, we conclude saying the delegate is who deals with the class events, we don't care what class that the delegate is.

2) Completion

Other very known way, completions are nothing more than a piece of code saved in memory which will be executed at some time. If we have two view controllers we can pass a callback from one to the other telling what must happen when some event is triggered. Take a look:

This callback is called when the viewDidLoad happens. The first view controller defines what must happen when the completions is made, so it may respond to the second view controller events.

3)Notification Centers

It is very similar to the Observer design pattern that is often used in Java projects. Basically the first view controller creates a new notification center instance from default and assigns the own view controller as its observer for some kinda event. The second view controller executes some code that triggers that notification center event observed by the first view controller, making it respond to the second view controller internal events.

Notice that first the initial view controller creates a new observer into the notification center and assigns its method answerViewDidLoad as a selector. When that event "View Did Load" is triggered, that method is executed.

The second view controller triggers this event in its viewDidLoad and then prints a message on the console.

4)RxSwift

This last way is not very used as the first two, but it is a very powerful mechanism that allows handling asynchronous tasks through your application. We will not cover all RxSwift concepts in this article. If you want more details, take a look at this article.

An observable is nothing more than a sequence of items that for each single one, the observer assigned to it takes some action. The subject is a specific type of observable which works either as an observable and an observer. With this, we can pass it as a parameter between two view controllers creating the observer callback inside the first view controller and make the observable events to be emitted by the second:

Notice that when we emit an event through the onNext method, the handler is executed by the first view controller, who defined the observer behaviors. RxSwift is a very powerful tool for dealing with multiple asynchronous events. You can install RxSwift via Cocoapods.

Conclusion

Now that you learned four ways to establish a communication between two UIViewControllers, you have four possible answers for that interview I did two years ago and you are ready to link how many screens you want. Hope you enjoyed ;)

iOS developer- WWDC19 scholarship winner- Blockchain enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store