gpt4 book ai didi

ios - 错误 : Cannot change background color of button

转载 作者:可可西里 更新时间:2023-11-01 02:18:19 25 4
gpt4 key购买 nike

当我点击一个按钮时,我会弹出一个按钮面板,按钮上有一个颜色的名称。当我单击该颜色时,下面的代码应将我的两个按钮更改为所选颜色,但现在我选择它时,它会打印 sender.currentTitle 的颜色,如 Optional("Red") .

除了当我点击按钮时,按钮的颜色没有改变。事实上,当我点击“红色”时,我调用的下方红色区域中的打印函数并未打印,因此未调用该函数。

当我点击其中一种颜色时我的代码:

@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
}

如您所见,我将颜色分开,因为我不知道如何不这样做。我怎样才能调用说 if 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
}

关于ios - 错误 : Cannot change background color of button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com