繁体   English   中英

如何使用快速字符串的值作为要编辑的UIObject的名称?

[英]How can I use the value of a swift string as the name of an UIObject to edit?

是否可以使用swift字符串的值编辑UIObject? 例如,如果我必须更改在一个快速的iOS项目的情节提要中创建的按钮的背景色,而我不能写下该名称,但是该名称是字符串的值。 然后可以更改按钮的背景色吗? 我尝试执行此操作的原因是,我有很多按钮在按下时会调用具有自己名称的参数的函数。 完成后,该功能会知道哪个按钮变为红色。 我已经尝试过这样的事情:

@IBOutlet weak var PressMeButtonOutlet: UIButton!
@IBAction func PressMeButtonAction(sender: AnyObject) {
    reColor("PressMeButtonOutlet")
}    


func reColor (buttonName: String) {

\(buttonName).backgroundColor = UIColor.redColor()

当UIButton触发IBAction ,它将发送自己的引用作为发送者。

因此,简单的方法是:

@IBAction func PressMeButtonAction(sender: UIButton) {
    reColor(sender)
}    


func reColor (button: UIButton) {

    button.backgroundColor = UIColor.redColor()
}

注意,我已经将IBAction参数更改为UIButton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM