Updated 25 March 2021
Notification Center use for broadcasting singles or notifying the methods at the same time. It uses the Observer pattern to inform registered observers when a notification comes in, using a central dispatcher called NotificationCenter.
Before we get started, some quick notes:
NotificationCenter
class used to be called NSNotificationCenter
– with the “NS” prefix – prior to Swift 3. You’ll often find people still referring to it as NSNotificationCenter.NotificationCenter
mechanism has nothing to do with push notifications, local notifications or the NotificationCenter UI, an overview of app notifications as found in iOS and macOS.NotificationCenter
to send in-app broadcasts with Swift.Let’s create a project to pass and receive data one controller to second controller from Notification Center.
2. In first controllerView , we add or remove Observer in:
1 2 3 4 5 6 7 8 9 10 11 |
override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector:#selector(facebook(notification:)), name: .facebook, object: nil) NotificationCenter.default.addObserver(self, selector:#selector(twitter(notification:)), name:.twitter , object: nil) } |
This is the notification registration process for receiving. Based on the key NotificationCenter
can observe.
The addObserver(_:selector:name:object:)
function has 5 parameters:
NotificationCenter.default
— This is the notification variable you can create it globally inside your class if you having more notifications.Observer
— This is for the class where we are going to observer notification.Object
— Inside the object, you can pass any object or any variable value (Bool, String, Dictionary, Array, Int
). I will explain to you on this topic. Currently, we set the nil value because we are not going to pass any values in this process.3. Now write functions you want to call when a notification is observed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@objc func facebook(notification: Notification){ label.text = "Facebook" imageview.image = imageLiteral(resourceName: "Facebook") } @objc func twitter(notification: Notification){ label.text = "Twitter" imageview.image = imageLiteral(resourceName: "simple") } |
function has one parameter, which is a Notification object. you can use userInfo
to send some data with the notification.
4. Every notification has a name of type Notification.Name.
1 2 3 4 5 6 7 |
extension Notification.Name{ static let facebook = Notification.Name("Facebook") static let twitter = Notification.Name("Twitter") } |
2. Now, add Notification Post in both button action.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@IBAction func facdebookAction(_ sender: Any) { NotificationCenter.default.post(name: .facebook, object: nil) self.navigationController?.popViewController(animated: true) } @IBAction func twitterAction(_ sender: Any) { NotificationCenter.default.post(name: .twitter, object: nil) self.navigationController?.popViewController(animated: true) } |
This will call the notification method from any place and changed the label and image view according to key. It will identify based on key the observer. You can also pass data with this post Notification.
For other blogs Please click here
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.