Ios 错误:无法更改按钮的背景色
当我点击一个按钮时,我会弹出一个按钮面板,上面有一种颜色的名称。当我单击该颜色时,下面的代码应将我的两个按钮更改为所选颜色,但现在我选择了它,它将打印sender.currentTitle的颜色,如Ios 错误:无法更改按钮的背景色,ios,string,swift,uicolor,sender,Ios,String,Swift,Uicolor,Sender,当我点击一个按钮时,我会弹出一个按钮面板,上面有一种颜色的名称。当我单击该颜色时,下面的代码应将我的两个按钮更改为所选颜色,但现在我选择了它,它将打印sender.currentTitle的颜色,如可选(“红色”) 除了单击按钮外,按钮的颜色不会改变。事实上,当我点击“Red”时,我调用的下面红色区域中的print函数没有打印,因此该函数没有被调用 单击其中一种颜色时的代码: @IBAction func button1ActualColorChosen(sender: AnyObject) {
可选(“红色”)
除了单击按钮外,按钮的颜色不会改变。事实上,当我点击“Red”时,我调用的下面红色区域中的print函数没有打印,因此该函数没有被调用
单击其中一种颜色时的代码:
@IBAction func button1ActualColorChosen(sender: AnyObject) {
var coliString1 = "\(sender.currentTitle!)"
print("\(coliString1)")
if coliString1 == "Black"{
Button1C.backgroundColor = UIColor.blackColor()
button1Bang.backgroundColor = UIColor.blackColor()
}
if coliString1 == "Red"{
print("red")
Button1C.backgroundColor = UIColor.redColor()
button1Bang.backgroundColor = UIColor.redColor()
}
if coliString1 == "Green"{
Button1C.backgroundColor = UIColor.greenColor()
button1Bang.backgroundColor = UIColor.greenColor()
}
if coliString1 == "Orange"{
Button1C.backgroundColor = UIColor.orangeColor()
button1Bang.backgroundColor = UIColor.orangeColor()
}
if coliString1 == "Blue"{
Button1C.backgroundColor = UIColor.blueColor()
button1Bang.backgroundColor = UIColor.blueColor()
}
if coliString1 == "Cyan"{
Button1C.backgroundColor = UIColor.cyanColor()
button1Bang.backgroundColor = UIColor.cyanColor()
}
if coliString1 == "Brown"{
Button1C.backgroundColor = UIColor.brownColor()
button1Bang.backgroundColor = UIColor.brownColor()
}
if coliString1 == "Gray"{
Button1C.backgroundColor = UIColor.grayColor()
button1Bang.backgroundColor = UIColor.grayColor()
}
if coliString1 == "Purple"{
Button1C.backgroundColor = UIColor.purpleColor()
button1Bang.backgroundColor = UIColor.purpleColor()
}
if coliString1 == "Magenta"{
Button1C.backgroundColor = UIColor.magentaColor()
button1Bang.backgroundColor = UIColor.magentaColor()
}
Button1C.alpha = 1
}
正如你所看到的,我把颜色分开,因为Idk怎么不这样做。如果coliString1==“Red”{}“等,我如何调用表示
的函数。该行至少有一个问题
var coliString1 = "\(sender.currentTitle!)"
由于sender.currentTitle!返回一个字符串,因此不需要使用字符串插值将其分配给变量coliString1。另外,使用switch语句可以帮助清理一些内容
@IBAction func button1ActualColorChosen(sender: AnyObject) {
let button = sender as! UIButton
let coliString1 = button.currentTitle
var newColor = UIColor()
if let cs1 = coliString {
switch cs1 {
case "Black":
newColor = UIColor.blackColor()
case "Red":
newColor = UIColor.redColor()
case "Green":
newColor = UIColor.greenColor()
case "Orange":
newColor = UIColor.orangeColor()
case "Blue":
newColor = UIColor.blueColor()
case "Cyan":
newColor = UIColor.cyanColor()
case "Brown":
newColor = UIColor.brownColor()
case "Gray":
newColor = UIColor.grayColor()
case "Purple":
newColor = UIColor.purpleColor()
case "Magenta":
newColor = UIColor.magentaColor()
default:
newColor = UIColor.clearColor()
}
}
Button1C.backgroundColor = newColor
button1Bang.backgroundColor = newColor
Button1C.alpha = 1
}
因此,您将Colisting1设置为“(sender.currentTitle!),然后检查它是否为“红色”、“黑色”或其他颜色。我是否遗漏了什么?不确定Switf和iOS,但我知道它是C系列,对吗?您应该在线检查1)字符串比较方法,以及2)为什么最好将字符串与方法而不是操作数进行比较(==,!=,等等)。希望这会有所帮助。我不想提供一个非常简短的答案,并受到社区的抨击。祝你好运:)。另外,Java使用的方法是.equals(“”);
比较字符串。只是一个关于查找内容的提示。@PhilC。我理解这种关注,这通常是一个好的观点,但实际上=
在Swift中是正确的。这不应该发生。您已经打开了发送者.currentTitle
的值,所以当您打印出来时,它应该是“红色”,而不是“可选的(“红色”)“。请尝试直接打印打印(sender.currentTitle!)
,然后发布结果。谢谢!我一定会考虑到这一点!