KVO:如何使用上下文识别特定的UILabel?

时间:2017-11-28 00:06:11

标签: swift key-value-observing

如果您将键值观察器添加到同一对象中的多个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

    }

}

0 个答案:

没有答案