0

I have 3 notifications:

NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification3"), object: nil)

And I have for each post one differents observer in view controller

First:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification1"), object: nil, queue: nil, using: updateUx)

Second:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification2"), object: nil, queue: nil, using: updateUx)

Third:NotificationCenter.default.addObserver(forName:NSNotification.Name("Notification3"), object: nil, queue: nil, using: updateUx)

updateUx func contain only a print of notification.

I only got my first notification I cannot catch the two other, I don't know why.

Badr Filali
  • 279
  • 5
  • 21
  • There's no way your first notification can be received. You are not observing to `Notification1`, but you are observing `DashboardNotification ` – viral Feb 24 '17 at 10:54
  • are you sure the names are to add observer and post observer are same ? – Priyal Feb 24 '17 at 10:56
  • Try changing `NotificationCenter.default.addObserver` like following. It should work. I tried it and it does. – viral Feb 24 '17 at 11:01
  • Sorry my bad, for posting this on github, I change the name, I edit my question. – Badr Filali Feb 24 '17 at 11:01
  • I am not facing any issue while using this code. Are you sure your observers are added and then notification is posted ? – Priyal Feb 24 '17 at 11:06
  • Check updated answer. If this doesn't work, I don't know what would. :) – viral Feb 24 '17 at 11:17
  • Where you remove observer? If you remove observer in viewWillDisappear, controller which currently not on screen will not receive any notification. – Bohdan Savych Feb 24 '17 at 11:21

1 Answers1

4

Add your observers like following:

NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)

And you are good to go.


EDIT: Full source code (This project has a UIButton on view and the @IBAction is connected to it in storyboard. On tapping that button, all 3 notifications will get posted. The log is supposed to get printed thrice in console)

class ViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)
    }

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

    @IBAction func abc (_ sender: UIButton) {
        NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil)
        NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil)
        NotificationCenter.default.post(name:NSNotification.Name("Notification3"), object: nil)
    }

    func updateUx(){
        print("received...")
    }
}
viral
  • 4,168
  • 5
  • 43
  • 68