[英]How can I use the value of a swift string as the name of an UIObject to edit?
Is it possible to edit an UIObject with the value of a swift string? 是否可以使用swift字符串的值编辑UIObject? If I for example have to change the backgroundcolor of a button created in the storyboard of a swift iOS project, and I cannot write the name, but that the name is the value of a string.
例如,如果我必须更改在一个快速的iOS项目的情节提要中创建的按钮的背景色,而我不能写下该名称,但是该名称是字符串的值。 Is it then possible to change the backgroundcolor of the button?
然后可以更改按钮的背景色吗? The reason I'm trying to do this, is that I have a lot of buttons that calls a function with the parameter of their own name when pressed.
我尝试执行此操作的原因是,我有很多按钮在按下时会调用具有自己名称的参数的函数。 When it's done the function knows which button to color red.
完成后,该功能会知道哪个按钮变为红色。 I have tried something like this:
我已经尝试过这样的事情:
@IBOutlet weak var PressMeButtonOutlet: UIButton!
@IBAction func PressMeButtonAction(sender: AnyObject) {
reColor("PressMeButtonOutlet")
}
func reColor (buttonName: String) {
\(buttonName).backgroundColor = UIColor.redColor()
When a UIButton triggers an IBAction
, it sends its own reference as the sender. 当UIButton触发
IBAction
,它将发送自己的引用作为发送者。
So the easy way to do it is this: 因此,简单的方法是:
@IBAction func PressMeButtonAction(sender: UIButton) {
reColor(sender)
}
func reColor (button: UIButton) {
button.backgroundColor = UIColor.redColor()
}
Notice that I have changed the IBAction parameter to UIButton
. 注意,我已经将IBAction参数更改为
UIButton
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.