Pass data using NSNotification in Swift

Muhammad Ariful Islam
Dev Genius
Published in
5 min readJun 27, 2020

--

Describe how to pass data between view controllers using notification patterns in the Swift programming language.

Overview

In iOS development, it is very common to share data between different views. We can pass data in different ways. In this tutorial, we will learn how to send data from one view controller to another using NSNotification.

This tutorial is written using Swift 5, Xcode 11.4, iOS 13 & Storyboard Interface.

What is Notification?

Notification is a way of notifying when sending data from one view to another. You can compare it with sending a letter using the postbox. Sender posted a letter into the postbox for one or more receivers. Postmaster of the receiver's area is observing for the letters to come. When the postmaster gets the letter, he/she do some specific work (distribute them to the actual receiver).

Sending data using notification is exactly similar to this process. Notifications are posted from one view controller. One or more view controllers are observing for that notification. When they notify, they do some specific work. Let's see how all these things are work together

Getting started with a simple project

I will use a simple single view app for this tutorial. This app will have only two view controllers, “FirstVC” & “SecondVC”. This is the storyboard interface for the project.

Here are the view controller classes for “FirstVC” & “SecondVC”. Our intention is to send notification from the “SecondVC” to “FirstVC” that's why we set labels in the “FirstVC” as following when view load.

We just set the views and write some initial codes for our project. In this stage, our app seems like this.

That’s our project set up and it's ready to configure, Cool 😎

Work with notification

Now we will work with notification. First of all, we will declare a unique key. One view controller will send the notification using that key, another view controller will observing the notification associated with that key and do something when notified. Let's see how everything works together.

1. Set a notification key:

For identifying every notification uniquely we need a unique notification key. Key can declare as a variable anywhere inside your module but make sure they are in a separate class or outside of other's class scope.

Here I declare a variable as “notificationKey” inside “FirstVC.Swift” file and outside of the “FirstVC” class. Using this key we will post and observe notification.

2. Set an Observer:

We wrote a function for observing the notification. Here self refers to who is observing the notification. In our case FirstVC. #Selector is what action will perform after notifying. In our case, it will execute the “doWhenNotify” function and set the “notificationTxtLbl” text if “FirstVC” successfully observed that notification. Lastly, the name is the unique key for the notification “FirstVC” is observing.

3. Post Notification:

We will post notification while tapping on the “submitBtnTap” button inside “SecondVC” as following. We pass “SecondVC” reference as an object while posting this notification.

If you follow everything according and run the project you should see something like this. Notice that “notificationTxtLbl” inside “FirstVC” text changes as we expected. That's means we successfully communicated using notification, great 😃

That’s the way that how two or more view controllers can talk to each other using notification. But something you have to send some information using notification also. Now we will see how we can do that.

Generally, we use a dictionary to send data using notification. We put information as key-value pair inside the dictionary and add that dictionary as a parameter while posting notification.

In our case, we take a dictionary named “info” and put the text field inputs inside it as a key-value pair. Add that dictionary into the post method as a parameter named “userInfo”.

Now we will use that “userInfo” parameter in “FirstVC” that sends while posting notification.

“userInfo” dictionary has two key, “name” & “age”. We safely unwrap those values for keys and set them into “infoLbl”. That’s how we can pass data using notifications. Now you can set your username, age, and pass them from “SecondVC” to “FirstVC” using notification.

Deinitialize notification

You can deinitialize notification in many ways. It can be done by deinit or view controller lifecycle function (viewDidDisappear or viewWillDissapear) function.

Congratulation 🎉 🎉 🎉 I guess now you know how to work with NSNotification in swift. You will find the project here. Check the Apple Documentation for further details.

If you found this article useful please share and give some clap 👏👏👏
Check my other articles on Medium and connect me on LinkedIn.

Thank you for reading & Happy coding 🙂

--

--