I have a problem with NotificationCenter in ios app.My app consists of two ViewControllers.
In the first ViewController, there are two buttons. ,Move to 2 ViewController, button makes transition to a second View Controller.
And ,Change to red color, button supposed to change color of UIView in second View Controller.
Here is code of first ViewController:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func changeToRed(_ sender: Any) {
NotificationCenter.default.post(name: NSNotification.Name("red"), object: nil)
}
}
Here is code of second ViewController:
import UIKit
class ViewController2: UIViewController {
@IBOutlet weak var colorfulView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(makeViewRed), name: Notification.Name("red"), object: nil)
}
@objc func makeViewRed() {
colorfulView.backgroundColor = UIColor.red
print("UIview color has been changed!")
}
}
When, I press ,change to red color, button I see message in console that UIView color has been changed. However, when I move to second ViewController, UIView color is unchanged. How can I solve this problem?