【问题标题】:Change text color of buttons from black to white and then revert back将按钮的文本颜色从黑色更改为白色,然后恢复
【发布时间】:2023-04-07 12:07:01
【问题描述】:

我在滑出式菜单中有十个按钮。如何通过选择将十个按钮的文本颜色从黑色更改为白色,然后在用户单击另一个按钮时恢复为原始状态。

我正在一个一个地循环按钮,我使用了代码

@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

    }

这种状态的问题是当我选择其他按钮时按钮颜色仍然是白色。当用户选择另一个按钮时,我想将文本颜色更改为黑色

当我点击按钮时,选择的控制状态在几秒钟内几乎不存在

【问题讨论】:

  • 所有这些按钮都不像单选按钮那样工作。在每次单击按钮时,您都应该恢复其他按钮的状态。
  • 我所有的按钮都以不同的方式命名。我应该在每个循环后恢复吗?谢谢

标签: ios swift uibutton uicontrolstate


【解决方案1】:

试试这个:

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
}

【讨论】:

  • 值得注意的是,循环越少效率越高。
  • @Losiowaty 确实如此。
【解决方案2】:

您可能希望为特定状态设置颜色,然后在需要时更改按钮状态。听起来你想要一个单选类型的按钮集,所以将按钮放在一个数组中,当点击任何一个按钮时,将该按钮的状态更改为“已选择”,将其他按钮的状态更改为“正常”或类似的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多