I am trying to observe the change in UIButton title using KVO pattern.Added observer in viewDidLoad
.
@IBOutlet weak var KVOBTn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
KVOBTn.titleLabel!.addObserver(self, forKeyPath: "btntest", options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old , context: nil)
}
This is the method that listens if there is any change in the title
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "btntest"{
KVOBTn.backgroundColor = UIColor.greenColor()
}
}
I have changed the button title through another button action
@IBAction func changeTitle(sender: AnyObject) {
KVOBTn.setTitle("testAgain", forState: UIControlState.Normal)
}
The thing is the observeValueForKeyPath
method is never being called.What am i doing wrong?