错误:无法更改按钮的背景颜色

时间:2015-12-17 01:52:38

标签: ios string swift uicolor sender

当我点击一个按钮时,我弹出一个按钮面板,按钮上有一个颜色的名称。当我点击该颜色时,下面的代码应该将我的两个按钮更改为所选的颜色,但是现在我选择它,它会打印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
}

正如你所看到的,我把颜色分开导致Idk如何不这样做。如何调用说if coliString1 == "Red"{}"等的函数

1 个答案:

答案 0 :(得分:1)

至少存在一个问题
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
}