0

I have a two ViewControllers: ViewController and SecondViewController. I added an observer to this two ViewControllers. In ViewController I also defined an IBAction to post the notification. I handle the notification via a closure in both ViewControllers. But only the closure in the ViewController gets called. The closure (and even the whole code) in the SecondViewController does not get called (I checked with debugger). The closure only contains a print-statement. Here is my Code

//ViewController
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let nc = NotificationCenter.default
        nc.addObserver(forName: Notification.Name(rawValue:"MyNotification"), object: nil, queue: nil) { (notification) in
            print("I'm the : \(type(of: self))")
        }
    }

    @IBAction func sendNotification(_ sender: UIButton) {
        let nc = NotificationCenter.default
        nc.post(name: Notification.Name(rawValue:"MyNotification"), object: nil, userInfo: ["message":"Hello there!", "Date:":Date()])

    }
}

The ScondViewController

//SecondViewController
import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let nc = NotificationCenter.default
        nc.addObserver(forName: Notification.Name(rawValue:"MyNotification"), object: nil, queue: nil) { (notification) in
            print("I'm the: \(type(of: self))")
        }
    }
}

The closure in ViewController gets called but the closure in SecondViewController does not. Maybe the reason is that SecondViewController does not get initialized before I post the notification. But how would a solution look like? Any help is appreciated.

user1895268
  • 1,559
  • 3
  • 11
  • 23
  • When you send/post the notification, is SecondViewController instance in memory? – Puneet Sharma Oct 31 '17 at 12:18
  • It's not gets called. You should post this notification with the delay. – Mannopson Oct 31 '17 at 12:20
  • Right, it's not get called, because the SVC is not initialized before i post. I added `let svc = SecondViewController()` in viewDidLoad in `ViewController` to call the default init but this has no effect. The closure in SVC is not getting called. Does anyone know a workaround? – user1895268 Oct 31 '17 at 12:27
  • I tried this: In the storyboard i added a second button `do segue` to perform a segue between the first and second Viewcontroller. I pushed the `do segue` button, went back and pressed the `Send Notification Button`. and this worked. So i know its an initialization issue. But i don't want to segue to my 5 or more ViewControllers before i post a notification, so what is a good solution? – user1895268 Oct 31 '17 at 12:42

1 Answers1

1

If this is one-to-one relationship you can also use the Delegate pattern instead. Although you are right, the notification observer is not yet called because you do not initialise before the post. So there shouldn't be a reason for a function to be called in the SecondViewController.

So if you only need that to update a variable value, you can do that somewhere else in a Variables class where both ViewControllers have access.

PoolHallJunkie
  • 1,345
  • 14
  • 33
  • yes i could do this in a very simple way. but in fact i have 5 ViewControllers i want to notify. i only posted the two classes because i wanted to keep it simple and to understand the basic mechanism. – user1895268 Oct 31 '17 at 12:20
  • @user1895268 It's hard to retrieve from rest of the viewControllers. Notification posted immediately – Mannopson Oct 31 '17 at 12:25
  • I found out how to solve it in a very simple way. The code for adding an observer should not be in viewDidLoad. Instead i made a class func `class func setObserver()` in `SecondViewController`. In my `ViewController` in `viewDidLoad` i just added `SecondViewController.setObserver()` and that does the trick. Thank you all for your comments – user1895268 Oct 31 '17 at 13:14