How do you add an observer in Swift to the notification center?
do it like this:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ReloadData), name: NSNotification.Name(rawValue: "ReloadData"), object: nil)
}
@objc func ReloadData(notification: NSNotification) {
// func
print ("FUNC TEST")
}
But every time the controller closes/opens (switch between the tabs of the tabbar), a new listener is added. And when I call
print ("Call Notif")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadData"), object: nil)
"func ReloadData" is called several times. Console:
Call Notif
FUNC TEST
FUNC TEST
will switch between the tabs of the tabbar again.
Call Notif
FUNC TEST
FUNC TEST
FUNC TEST
How can I oblige you only once ?