Swift - NSNotificationCenter

NSNotificationCenter allows us to send and receive information between classes and/or structs based on an action that has occurred in our app. Rather, it is a communication tool internal to your app.
NotificationCenter existed since the first release of OSX which is atleast 17 years, and has been a popular tool for Apple developers ever since. For those who dont know it's based off the Observer patter concept which is part of the behavioural group of software design patterns.

NSNotifications are both simple and powerful, which is why they show up so often in the frameworks. Specifically, they have a few distinct advantages:
  1. Loose coupling between notification senders and receivers.
  2. Support for multiple receivers for a single notification.
  3. Support for custom data on the notification using the userInfo property.
Let's create a simple app that demonstrates the use of NSNotificationCenter. In the app i have embedded a navigation controller hooked up any IBOutlets and IBActions needed. The goal of this app is to display the selected color from a different view controller
Source and destination controller 


Begin by creating actions that will be performed whenever NSNotificationCenter has detected a change. In our case we are creating two functions which displays the selected color in our ViewController
    @objc func redSelection(notification: NSNotification) {
        colorLabel.text = "Selected Color: Red"
    }
    
    @objc func blueSelection(notfication: NSNotification) {
        colorLabel.text = "Selected Color: Blue"
    }

Create an extension on Notification.Name and added two static properties with the name of each channel.


    extension Notification.Name {
        static let redColor = Notification.Name("red")
        static let blueColor = Notification.Name("blue")
    }

Add observation methods in ViewDidLoad that will listen to their designated channels to perform actions 

NotificationCenter.default.addObserver(self, selector: #selector(redSelection(notification:)), name: .redColor, object: nil)
        
NotificationCenter.default.addObserver(self, selector: #selector(blueSelection(notfication:)), name: .blueColor, object: nil)

As per the selected function the event that a notification has been posted to the respective channels, the same is then applied to each of the function that listens for and changes the label's text 

Move to next firstViewController and post the notification when someone clicks on either button.

    @IBAction func redColorSelected(sender: UIButton) {
        NotificationCenter.default.post(name: .redColor, object: nil)
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func blueColorSelected(sender: UIButton) {
        NotificationCenter.default.post(name: .blueColor, object: nil)
        self.navigationController?.popViewController(animated: true)
    }

we will post notification to .redColor channel if user clicks on redColor button, and same applies for .blueColor channel

Now run the app, see the resultant appears as below


Comments