如何使用观察者?为什么我的观察员不在这里工作?
How to use observer ? Why my observer is not working here?
我的项目中有两个视图控制器和两个视图控制器class。我想使用通知和观察者从第二个视图控制器更改第一个视图控制器的背景颜色。但它不起作用。
我注意到 "changeViewControllerColor(_:)" 方法没有调用。
第一个视图控制器:
import UIKit
let colorChangeNotificationKey = "changeFirstVcColor"
class FirstViewController: UIViewController {
let notiName = Notification.Name(rawValue: colorChangeNotificationKey)
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
observer()
}
func observer() {
NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeViewControllerColor(_:)), name: self.notiName, object: self)
}
@objc func changeViewControllerColor(_: NSNotification) {
self.view.backgroundColor = UIColor.white
}
@IBAction func button(_ sender: UIButton) {
let vc = storyboard?.instantiateViewController(identifier: "secondViewController") as! SecondViewController
navigationController?.pushViewController(vc, animated: true)
}
}
第二个视图控制器:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "First VC colour is white now"
let notiName = Notification.Name(rawValue: colorChangeNotificationKey)
NotificationCenter.default.post(name: notiName, object: nil)
}
}
当您添加观察者时,您正在将对象自身传递给它。
你可能想传递一个 nil。
来自文档:
anObject
that is, only notifications sent by this sender are delivered to the
observer.
If you pass nil, the notification center doesn’t use a notification’s
sender to decide whether to deliver it to the observer.The object whose notifications the observer wants to receive;
所以它唯一会接受通知的是它自己,这可能不是你想要的。
此外,我同意 Harish 的意见,你应该使用委托。
在 SecondViewController 中:
NotificationCenter.default.post(name: Notification.Name("changeFirstVcColor"), object: nil)
在 FirstViewController 中:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("changeFirstVcColor"), object: nil)
@objc func methodOfReceivedNotification(notification: Notification) {}
我的项目中有两个视图控制器和两个视图控制器class。我想使用通知和观察者从第二个视图控制器更改第一个视图控制器的背景颜色。但它不起作用。 我注意到 "changeViewControllerColor(_:)" 方法没有调用。
第一个视图控制器:
import UIKit
let colorChangeNotificationKey = "changeFirstVcColor"
class FirstViewController: UIViewController {
let notiName = Notification.Name(rawValue: colorChangeNotificationKey)
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
observer()
}
func observer() {
NotificationCenter.default.addObserver(self, selector: #selector(FirstViewController.changeViewControllerColor(_:)), name: self.notiName, object: self)
}
@objc func changeViewControllerColor(_: NSNotification) {
self.view.backgroundColor = UIColor.white
}
@IBAction func button(_ sender: UIButton) {
let vc = storyboard?.instantiateViewController(identifier: "secondViewController") as! SecondViewController
navigationController?.pushViewController(vc, animated: true)
}
}
第二个视图控制器:
import UIKit
class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label.text = "First VC colour is white now"
let notiName = Notification.Name(rawValue: colorChangeNotificationKey)
NotificationCenter.default.post(name: notiName, object: nil)
}
}
当您添加观察者时,您正在将对象自身传递给它。
你可能想传递一个 nil。
来自文档:
anObject
that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.The object whose notifications the observer wants to receive;
所以它唯一会接受通知的是它自己,这可能不是你想要的。
此外,我同意 Harish 的意见,你应该使用委托。
在 SecondViewController 中:
NotificationCenter.default.post(name: Notification.Name("changeFirstVcColor"), object: nil)
在 FirstViewController 中:
NotificationCenter.default.addObserver(self, selector: #selector(self.methodOfReceivedNotification(notification:)), name: Notification.Name("changeFirstVcColor"), object: nil)
@objc func methodOfReceivedNotification(notification: Notification) {}