点按以选择uibutton,然后点按以取消选择uibutton

时间:2016-06-28 18:26:43

标签: ios uibutton swift2 xcode7 selected

您好我正在尝试实施以下

点击uibutton选择并突出显示背景,然后再次点击相同的按钮取消选择uibutton背景为原始状态或其他颜色

我的代码如下:

@IBOutlet weak var case4Btn: UIButton!

@IBAction func case4BtnClicked(sender: AnyObject) { //touch up inside
    case4Btn.backgroundColor = UIColor.cyanColor()
}
@IBAction func case4BtnCancel(sender: AnyObject) {
    case4Btn.backgroundColor = UIColor.lightGrayColor()//touch down
}

当我点击一次选择并突出显示UIButton按钮时,我再次点击它会改变颜色但是没有取消选择,以便我取消选择我必须点击,按住并拖离按钮改变颜色或恢复原状

请帮忙,因为这让我很生气,似乎很简单的东西似乎很难

提前谢谢

3 个答案:

答案 0 :(得分:1)

解决方案1 ​​

通过代码或InterfaceBuilder设置按钮的/全部状态颜色,文本,文字颜色,如下所示

button.setBackgroundImage(UIImage(named: "cyanColorImage"), forState: .Normal)
button.setBackgroundImage(UIImage(named: "brownColorImage"), forState: .Selected)

并处理目标并仅更改按钮的状态

@IBAction func buttonClickedHandle(sender: UIButton)
{
    sender.selected = !sender.selected
}

解决方案2

您可以在没有额外变量的情况下执行此操作。实现这个目标

  • 您可以使用按钮的selected属性来满足您的要求。
  • 您还可以使用单一属性处理按钮样式。
  • 您不需要为所需的所有按钮编写单独的方法。只需为所有人​​编写单一方法。

    @IBAction func buttonClickedHandle(sender: UIButton)
    {
        if sender.selected
        {
            sender.backgroundColor = UIColor.cyanColor()
        }
        else
        {
            sender.backgroundColor = UIColor.lightGrayColor()
        }
    
        sender.selected = !sender.selected
    }
    

将所有按钮的目标添加到buttonClickedHandle,然后将该特定按钮作为sender进行访问。您正在为所有按钮执行相同的任务,然后为什么不按照说明重复使用代码。

一切顺利!

答案 1 :(得分:0)

唯一合法的姿态是Touch Up Inside。完全删除您的其他操作。使用Bool属性来跟踪按钮所处的状态。当点击它时,使用if语句根据Bool属性更改按钮的背景颜色 - 并将Bool属性更改为匹配新的州。

@IBOutlet weak var case4Btn: UIButton!
var gray = true
@IBAction func case4BtnClicked(sender: AnyObject) { //touch up inside
    if gray {
        case4Btn.backgroundColor = UIColor.cyanColor()
    } else {
        case4Btn.backgroundColor = UIColor.lightGrayColor()
    }
    gray = !gray
}

答案 2 :(得分:0)

对于1个按钮

var buttonState = "cyan"
//the color the button should be when pressed

@IBOutlet weak var case4Btn: UIButton!
//the button

@IBAction func case4BtnClicked(sender: AnyObject) {
    //touch up inside
    if(buttonState == "cyan"){
        //if the button is supposed to be cyan
        case4Btn.backgroundColor = UIColor.cyanColor()
        //set the background color
        buttonState = "gray"
        //set it to be gray next time
    }
    else{
        //if it isn't
        case4Btn.backgroundColor = UIColor.grayColor()
        //set the background color
        buttonState = "cyan"
        //make it become cyan next time
    }
}

多个按钮

var button1State = "cyan"
var button2State = "cyan"
var button3State = "cyan"
//the color the buttons should be when pressed

@IBOutlet weak var case4Btn1: UIButton!
@IBOutlet weak var case4Btn2: UIButton!
@IBOutlet weak var case4Btn3: UIButton!
//the buttons

@IBAction func case4Btn1Clicked(sender: AnyObject) {
    //touch up inside
    if(button1State == "cyan"){
        //if the button is supposed to be cyan
        case4Btn1.backgroundColor = UIColor.cyanColor()
        //set the background color
        button1State = "gray"
        //set it to be gray next time
    }
    else{
        //if it isn't
        case4Btn1.backgroundColor = UIColor.grayColor()
        //set the background color
        button1State = "cyan"
        //make it become cyan next time
    }
}
@IBAction func case4Btn2Clicked(sender: AnyObject) {
    //touch up inside
    if(button2State == "cyan"){
        //if the button is supposed to be cyan
        case4Btn2.backgroundColor = UIColor.cyanColor()
        //set the background color
        button2State = "gray"
        //set it to be gray next time
    }
    else{
        //if it isn't
        case4Btn2.backgroundColor = UIColor.grayColor()
        //set the background color
        button2State = "cyan"
        //make it become cyan next time
    }
}
@IBAction func case4Btn3Clicked(sender: AnyObject) {
    //touch up inside
    if(button3State == "cyan"){
        //if the button is supposed to be cyan
        case4Btn3.backgroundColor = UIColor.cyanColor()
        //set the background color
        button3State = "gray"
        //set it to be gray next time
    }
    else{
        //if it isn't
        case4Btn3.backgroundColor = UIColor.grayColor()
        //set the background color
        button3State = "cyan"
        //make it become cyan next time
    }
}