Build an iOS app with NotificationCenter
A notification dispatch mechanism that enables the broadcast of information to registered observers.
Source: Apple Documentation
Surely you have worked with communication patterns before. One example for a communication pattern is the delegation pattern. The delegation pattern is a one- to- one relationship, meaning one class communicates to another. In contrast, the NotificationCenter 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.
Download the starter project here.
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.