Swift: Determine if the buttons title was changed

帅比萌擦擦* 提交于 2020-01-17 06:28:40

问题


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.

thoughts?


回答1:


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)")
}


来源:https://stackoverflow.com/questions/38989264/swift-determine-if-the-buttons-title-was-changed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!