[英]declare NotificationCenter.default.addObserver [swift]
How do you add an observer in Swift to the notification center?如何在 Swift 中将观察者添加到通知中心?
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. “func ReloadData”被多次调用。 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 ?我怎么能只答应你一次?
I have a similar situation as yours.我和你有类似的情况。 Having read https://stackoverflow.com/a/60399071/14414215 and https://stackoverflow.com/a/51745479/1040347 and trying it out, it didn't work for me in my particular scenario whereby I am using a UITabBarController design to pass data to to different tabs.阅读https://stackoverflow.com/a/60399071/14414215和https://stackoverflow.com/a/51745479/1040347并试用后,它在我使用的特定场景中对我不起作用UITabBarController 设计用于将数据传递到不同的选项卡。
Tab1 is the main screen Tab2 is the library of workouts (user to select the data) Tab1 是主屏幕 Tab2 是锻炼库(用户选择数据)
Then using the TabBarController as the intermediate https://stackoverflow.com/a/65499693/14414215 to pass data from Tab2 to Tab1.然后使用 TabBarController 作为中间https://stackoverflow.com/a/65499693/14414215将数据从 Tab2 传递到 Tab1。 When adding the removeObserver in Tab1在 Tab1 中添加 removeObserver 时
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
This will result in Data not getting passed from Tab2 to Tab1 as the notification was unsubscribed whenever user moves from Tab1 to Tab2.这将导致数据不会从 Tab2 传递到 Tab1,因为每当用户从 Tab1 移动到 Tab2 时,通知就会取消订阅。
I also tried to add the removeObserver into the TabBarController but this didn't do anything.我还尝试将 removeObserver 添加到 TabBarController 中,但这没有做任何事情。
In the end, what worked was to unsubscribe from within the objc eg:最后,有效的是从 objc 中取消订阅,例如:
in Tab1:在表 1 中:
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")
NotificationCenter.default.removeObserver(self). // Adding it here works and its then only called ONCE
}
Like it or not, the simplest solution for that is to make use of the controller's life cycle.不管喜欢与否,最简单的解决方案是利用控制器的生命周期。
Remove notification for every deinit
or disappearance of the controller.删除每次deinit
或控制器消失的通知。
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadData"), object: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self) }
If you have a base class for each and every controllers of yours, you can put the removeObserver()
in that base class.如果您的每个控制器都有一个基类,则可以将removeObserver()
放在该基类中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.