Back to Home

Send and Receive Messages Through NSNotificationCenter

Shane Qi • 2016-03-24 22:43

Some function in class A sends a notification named ISentANotification:

class A {
    func sendNotification() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "ISentANotification"), object: self)
    }
}

When class B receives the notification named ISentANotification, calls the function IReceiveANotification:

class B: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        NotificationCenter.default.addObserver(self, selector: #selector(B.IReceiveANotification), name: Notification.Name(rawValue: "ISentANotification"), object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self)
    }

    @objc func IReceiveANotification() {
        print("I received a notification!")
    }

}