【发布时间】:2014-11-06 12:31:23
【问题描述】:
当我使用 KVO 时,我可以使用函数 observeValueForKeyPath(...) 的 change 参数来知道值。但是当有多个按钮添加观察者时,我怎么知道哪个按钮被改变了?
例如如下:
check1.addObserver(self, forKeyPath: "cell.state",
options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old,
context: nil)
check2.addObserver(self, forKeyPath: "cell.state",
options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old,
context: nil)
override func observeValueForKeyPath(keyPath: String, ofObject: AnyObject,
change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void> {
if keyPath == "cell.state" {
// I can get the value as follow, but how to know thevalue which button of?
if change[NSKeyValueChangeNewKey]?.boolValue == true {
self.isChecked = true
} else {
self.IsChecked = false
}
}
【问题讨论】:
标签: ios macos cocoa swift key-value-observing