Oma
Oma

Reputation: 31

tap to select uibutton then tap to deselect uibutton

Hi I am trying to implement the following

tap uibutton to select and highlight background then tap the same button again to deselect the uibutton background to original state or another color

my code is as follows:

@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
}

with the following code when I tap once it selects and highlights the UIButton button when i tap again it changes color buton does not deselect, in order for me to deselect I have to tap,hold and drag away from the button for it to change color or return to original state

Please help as this is driving me mad, something that seems to be so simple seems to be so hard

Thank you in advance

Upvotes: 0

Views: 2326

Answers (4)

@IBAction func tipChanged(_ sender: UIButton) {    
            tenPctButton.isSelected = false
            twentyPctButton.isSelected = false
            zeroPctButton.isSelected = false
            sender.isSelected = true
}

Upvotes: -1

Amit Singh
Amit Singh

Reputation: 2698

Solution 1

Set button's both/all state's color, text, text color either from code or from InterfaceBuilder as below

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

and handle the target and change the button's state only

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

Solution 2

You can do this with no extra variable. To achieve this

  • You can use button's selected property to achieve your requirement.
  • You can also handle button style with the single property.
  • You will not required to write separate method for all the button you need. Just write single method for all.

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

Add all your button's target to buttonClickedHandle and access that particular button as sender. You are doing same task for all your button then why not reuse the code as explained.

All the best!

Upvotes: 2

Marco
Marco

Reputation: 2314

For 1 Button

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
    }
}

For Multiple Buttons

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
    }
}

Upvotes: 0

matt
matt

Reputation: 534885

The only legitimate gesture is Touch Up Inside. Delete your other action entirely. Use a Bool property to keep track of which state the button is in. When it is tapped, use an if statement to change the button's background color according to the Bool property — and change the Bool property to match the new state.

@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
}

Upvotes: 0

Related Questions