【问题标题】:how to change dynamic button background color如何更改动态按钮背景颜色
【发布时间】:2014-12-26 03:41:06
【问题描述】:

我以编程方式创建了按钮,希望在单击按钮时更改颜色,例如,如果单击一个按钮将颜色更改为红色,单击另一个按钮意味着更改回默认值

我使用

创建了按钮
 for var i=0 ;i<=8;i++{
 var button = UIButton(..)
 ........... blah blah......

button.addTarget(..,action:"buttonsClick",...)

}

 fun buttonsClick(sender:UIButton){
    // here how to identify which button is clicked and change color to red if another is clicked means change to default color
    var tagnum = sender.tag

    }

或其他可用的解决方案

【问题讨论】:

    标签: button dynamic swift colors background


    【解决方案1】:

    我从 stackoverflow 的另一个讨论中得到了输出

    创建一个函数

    func Changecolor(sender:UIButton){

    如果 sender.backgroundColor == UICcolor.graycolor() {

    sender.backgroundColor = UIColor.redcolor()

    } 别的 {

    sender.backgroundColor = UIColor.graycolor()

    }

    这个函数在

    中调用

    button.addTarget(blah,selector:"Changecolor:",forControlEvents:UIControlEvents.AllEvents)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多