簡體   English   中英

將按鈕的文本顏色從黑色更改為白色,然后還原

[英]Change text color of buttons from black to white and then revert back

滑出菜單中有十個按鈕。 如何通過選擇將十個按鈕的文本顏色從黑色更改為白色,然后在用戶單擊另一個按鈕時恢復為原始狀態。

我一個接一個地遍歷按鈕,並使用了代碼

@IBAction func onBtnClick(sender: UIButton) {

        hmImg.hidden = true
    editprofileImg.hidden = true
    cntctsReqImg.hidden = true
    cntctsManImg.hidden = true
    preferencesImg.hidden = true
    timeRuleImg.hidden = true
    helpImg.hidden = true
    logoutImg.hidden = true


    var a: NSInteger = sender.tag

    if a == 1
    {


        homeBtn.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
        hmImg.hidden = false


        }

    else if a == 2
    {
        editProfileBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        editprofileImg.hidden = false

    }
   else if a == 3
    {
        cntctsRequetsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        cntctsReqImg.hidden = false

    }

   else if a == 4


    {
        cntctMangBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
         cntctsManImg.hidden = false

    }
 else   if a == 5
    {
        preferenceBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        preferencesImg.hidden = false
               }

  else  if a == 6
    {
        timeruleBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        timeRuleImg.hidden = false

    }

  else  if a == 7
    {
        helpBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        helpImg.hidden = false

    }


  else  if a == 8
    {
        logoutBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)

        logoutImg.hidden = false

    }

這種狀態的問題是,當我選擇其他按鈕時,按鈕顏色仍為白色。 我想在用戶選擇另一個按鈕時將文本顏色更改為黑色

當我單擊按鈕時,幾秒鍾內幾乎沒有用於選擇的控制狀態

嘗試這個:

let buttons = [homeBtn, editProfileBtn, ...]
let images [hmImg, ....]
func selectItemAtIndex(index:Int) {
    buttons.forEach {
      $0.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
   }  
   images.forEach {
       $0.hidden = true 
   }  
   buttons[index].setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
   images[index].hidden = false   
}
@IBAction func onBtnClick(sender: UIButton) {
    let index = sender.tag
    selectItemAtIndex(index - 1) // your tags is 1 based.
}

或者,您可以使用其他方式來減少循環。

let images [hmImg, ....]
var currentBtn: UIButton!
var currentImg: UIImageView!
@IBAction func onBtnClick(sender: UIButton) {
   currentBtn?.setTitleColor(UIColor.blackColor(), forState:UIControlState.Normal)
    sender.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
    currentBtn = sender
    let index = sender.tag - 1 // one based tages
    currentImg?.hidden = true
    currentImg = images[index]
    currentImg.hidden = false
}

您可能想為特定狀態設置顏色,然后在需要時更改按鈕狀態。 聽起來像是要設置單選按鈕,所以將按鈕排列在陣列中,並在輕按任意一個按鈕時,將該按鈕的狀態更改為“已選擇”,將其他狀態更改為“正常”或類似的狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM