Pass Data with NSNotification in Swift

Between ViewControllers

Bob Lee
Bob the Developer

--

What a great photo for NSNotification

Last update on May 14th, 2017 | Swift 3.1

2017 is coming, and Christmas is coming and most of my friends are taking final exams and I’m writing Swift articles. Just random thoughts before I start grinding.

Now, let’s talk about how and why we use NSNotification to pass data and communicate/notify between classes and structs. By the way, NSNotification, unlike its name, has nothing to do with Push Notification — rookie mistake 101 (me).

Yes, there are many ways to pass data in Swift: Delegate, Key-Value Observing, Segue, and NSNotification, Target-Action, Callbacks. However, each one has its own pros and cons. They are like dining utensils. For example, I wouldn’t use chopsticks — although it’s pretty darn good — to eat soup.

Since there are many, I’m only going to cover one at a time, and I will going to mention why you should spoons instead of chopsticks along the way. It doesn’t make sense to tell the differences between chopsticks and knives before I have used both.

What I think you will learn

In this tutorial, you will learn 2 things. First, you will understand how to “notify” other view controllers or even pass from a single view controller. Second, you will grasp the power of NSNotification and its weaknesses.

UI Component

There are two view-controllers. FirstVC and SecondVC. I assume you already know how to embed UINavigationController and connect IBOutlets and IBActions and so on.

SecondVC will notify FirstVC. When I say “notify”, it’s like poking. It’s not sending any data, but certainly, we can. I will explain how to send data a bit later in this article. The example below is analogous to a user making a profile update on Facebook or Instagram. I’m not using UITableView since that would be overkill for explaining the concept.

SecondVC (Right) notifying FirstVC (Left)

Before we jump in, let’s picture how we would implement this at an extremely high level. Imagine two viewcontrollers are like a beautiful couple. They both have smartphones (NSNotification Objects) to talk to each other. Second, each smartphone has two features: receiving and sending data. Lastly, to locate each other’s device, they have a common secret key. However, it’s up to each other whether one wants to pick up the call or simply ignore.

Since we have a general understanding how they communicate with each other, let’s dive into Swift ☝️

First, we are going to store the secret key. You can make a separate Swift file or just create one outside of any view-controller like this.

import UIKitlet myNotificationKey = "com.bobthedeveloper.notificationKey"class SecondVC: UIViewController {}

myNotificationKey will be used to connect those smartphones together. Of course, just like some other couples, you can have more than one key for whatever purposes. 🙃

Now, it’s time attach a smartphone. Let’s call this an observer. The observer will have four parameters. It will ask Observer which will be self since you are attaching the smartphone to SecondVC. Second, Selector, which is a function that runs when you notify. Third, name which refers to the secret code. Lastly, object which I will explain later when dealing with FirstVC. Just put nil for now.

secondVC

I don’t get the meaning of thedefault type property because there is no description in the API guideline. It says,

“No overview available” — Apple

Anyway, SecondVC has a smartphone/observer, it’s time to send/notify when the button is tapped

Tap to notify

The content has been migrated to the personal blog. If you wish to learn how to pass data between view controllers using NSNotification, please visit here.

--

--