Search code examples
iosswiftnotificationcenter

NotificationCenter is not working first time when it is posted from viewcontroller to another ViewController


I have 2 ViewControllers called ViewController1 and ViewController2.

I am posting notification from ViewController1 to ViewController2, but at first time when it is posting, at first time NotificationCenter is not working.

When I came back from ViewController2 to ViewController1 and then again if try to move ViewController2 then that time NotificationCenter it is working,

// ViewController1.swift

import UIKit

class ViewController1: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

//Move to next VC
@IBAction func nextButtonClicked(_ sender: Any) {

    NotificationCenter.default.post(name: Notification.Name("callMethodPrint1FromVC2"), object: nil)

    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let vc2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2Id") as? ViewController2
    navigationController?.pushViewController(vc2!, animated: true)

  } 
}

// ViewController2.swift

import UIKit

class ViewController2: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.


}

override func viewWillAppear(_ animated: Bool) {
    //Recieve notification
    NotificationCenter.default.addObserver(self, selector: #selector(self.print1Method(notification:)), name: Notification.Name("callMethodPrint1FromVC2"), object: nil)
}

@objc func print1Method(notification: Notification) {

    print("Notification came from VC 1")
}

@IBAction func backToVC1(_ sender: Any) {

    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let vc2 = storyBoard.instantiateViewController(withIdentifier: "ViewController1Id") as? ViewController1
    navigationController?.pushViewController(vc2!, animated: true)
}

 //Remove notification object
deinit {

    NotificationCenter.default.removeObserver(self, name: Notification.Name("callMethodPrint1FromVC2"), object: nil)
}

}

Actual Output:

When I move ViewController1 to ViewController2 at first time and every time, print1Method method from ViewController2 has to call.

But it is not working as expeted. Is there Anu issues in my code or I am missing something?


Solution

  • you are posting notification before your view controller is initialized.

    there is no need to post notification for calling the method of vc2. instead, directly call the method.

    In VC1

    @IBAction func nextButtonClicked(_ sender: Any) {
    
         let storyBoard = UIStoryboard(name: "Main", bundle: nil)
         let vc2 = storyBoard.instantiateViewController(withIdentifier:"ViewController2Id") as ViewController2
         vc2.print1Method()
         navigationController?.pushViewController(vc2!, animated: true)
    
      } 
    }
    

    In VC2

    func print1Method() {
    
         print("Method called from VC 1")
     }
    

    also for the back button, you are doing it wrong. simply pop the view controller from the stack

     @IBAction func backToVC1(_ sender: Any) {
    
         self.navigationController?.popViewController(animated: true)
     }