如果您将键值观察器添加到同一对象中的多个UILabels
,您如何在KVO上下文中专门识别它们?
将KVO添加到两个UILabels
:
someLabel.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
someOtherLabel.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
KVO处理程序:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if change?[.oldKey] as! String != change?[.newKey] as! String {
// do something
}
}