In the first viewcontroller added an observer who changes the text label

 NotificationCenter.default.addObserver(self, selector: #selector(updatePrice), name: Constants.NotificationUpdatePrice, object: nil) ... @objc func updatePrice(){ self.recomendLabel.text = "11111" } 

In the second viewController I show the first one and send the green light to the observer.

 self.navigationController?.pushViewController(viewcontroller, animated: true) NotificationCenter.default.post(name: Constants.NotificationUpdatePrice, object: nil) 

The problem is that when the first viewcontroller shows the text it remains unchanged, after a bit I realized that the updatePrice method updatePrice called earlier than, for example, the viewWillAppear method and because of this the text returns to the original one.

How can this problem be solved ?

    1 answer 1

    Change the text not to the constant "1111", but to a variable. The updatePrice () function, however, also changes the value of a variable. Call the NotificationCenter.default.post method after the first UIViewController is shown using the pushViewController method (swap the lines). In this case, the viewWillAppear method will display the correct data.

    1st UIViewController:

     var text = "00000" @objc func updatePrice() { text = "11111" } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.recomendLabel.text = text } 

    2nd UIViewController:

     NotificationCenter.default.post(name: Constants.NotificationUpdatePrice, object: nil) self.navigationController?.pushViewController(viewcontroller, animated: true)