Led Led - 1 year ago 52
Swift Question

Swift: Determine if the buttons title was changed

Im a newbie in swift, please bear with me.

I have button-A that when clicked show a popover, the popover has a text field and a confirm button. when the confirm is clicked, any value on the text field will be used to change the button-A title.

My question is, how can we monitor if the button title was changed? Obviously We can see it visually but my goal is to do it programatically.


Answer Source

An example might help a little here. If a button name myButton I can observe those attributes with:

let options = NSKeyValueObservingOptions([.New, .Old, .Initial, .Prior])

self.addObserver(self, forKeyPath: "myButton.titleLabel.text", options: options, context: nil)

// Changes to these properties will trigger a call to:

override func observeValueForKeyPath(keyPath: String!,
    ofObject object: AnyObject!,
    change: NSDictionary!,
    context: CMutableVoidPointer) {

        println("CHANGE OBSERVED: \(change)")