Build an iOS app with NotificationCenter

Use Case

Surely you have worked with communication patterns before. One example for a communication pattern is the . The delegation pattern is a one- to- one relationship, meaning one class communicates to another. In contrast, the pattern is distinguished as being one- to- many. As an example, think of a button within your app that, when pressed, changes an image on all your View Controllers.

AnimeFight App

What we are going to build

We have two View Controllers called BaseScreen and SelectionScreen. Based on the button pressed on the SelectionScreen, the image, label, and the color of the BaseScreen change.

Step 1: Assign a name to the notifications

We create the names of the notifications that we want to communicate. There are two occasions in which we want to do that — either when the dbz button is pressed or when the opm button is pressed. It is much safer to not keep Strings that can be mistyped within your project, so let’s create a constants file in which we store those strings.

Let’s go back to our BaseScreen and make changes accordingly:

Step 2: Register the BaseScreen as the observer of the notifications

In this step, we assign the BaseScreen as the receiver of the notifications. The inbuilt addObserver(_:selector:name:object:) method accepts four arguments:

addObserver(self, — add the BaseScreen as the observer/receiver of the notification

selector: #selector(setToGoku(_:)) — this is the function that will be fired every time the BaseScreen receives a .dragonball notification. We haven’t defined this function yet but will do so shortly

name: dragonballNotification — the name of the notification

object: nil — you can listen to this notification from a specific object and only handle that specific case but we use nil because it’s going to listen for this notification irrespective of which object sends it

Step 3: Fire notification to the observer

post(name: .dragonballNotification — name of the notification that will be sent to the receiver

object: nil — again, this allows to target a specific object to send this notification to. If we entered an object here, only this object would receive the notification. By entering nil, we broadcast it, creating a relationship that can be one-to-many.

userinfo: userInfo — this is optional. In this case, we could also have set the image, the color and the name in the BaseScreen and just send an empty notification to notify the receiver to change those properties. But i want to show you, how you can use a dictionary to store and send information. The dictinary is of type [String:Any] . We store a unique String, the key, with which we access the values later in the BaseScreen

Step 4: Create the functions that are fired once the BaseScreen is notified

We have to downcast the values we are getting out of the dictionary because we receive the notification as a notification type.

Step 5: Update the UI

The full project can be found here. If you have any questions, you can contact me on Twitter.



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