Swift:确定按钮标题是否已更改 [英] Swift: Determine if the buttons title was changed

查看:22
本文介绍了Swift:确定按钮标题是否已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,请多多包涵.

Im a newbie in swift, please bear with me.

我有按钮-A,当点击它时会显示一个弹出框,弹出框有一个文本字段和一个确认按钮.当点击确认时,文本字段上的任何值都将用于更改按钮-A 标题.

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.

想法?

推荐答案

这里的一个例子可能会有所帮助.如果按钮名称为 myButton,我可以通过以下方式观察这些属性:

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

这篇关于Swift:确定按钮标题是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆