2014-10-30 107 views
-1

我已編程方式創建按鈕,想改變顏色,當按鈕點擊,例如,如果一個按鈕,點擊改變顏色爲紅色,另一個按鈕點擊方式改回默認如何改變動態按鈕的背景顏色

我創建使用

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 

    } 
可用

或者另一種解決方案這個

回答

0

按鈕,我從另一個討論中輸出在計算器

創建函數

FUNC Changecolor(發件人:的UIButton){

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

sender.backgroundColor = UIColor.redcolor()

} 別的 {

sender.backgroundColor = UIColor.graycolor()

}

該函數調用在

button.addTarget(嗒嗒,選擇器: 「Changecolor:」,forControlEvents:UIControlEvents.AllEvents)