I have two UIButtons that I want to use to set an A/B value to a variable before I save data to a database. I want a button to become selected when tapped, and deselected when the other button is tapped, and vice versa. What is a good solution for accomplishing this programmatically or in Interface Builder?
In order to set an "A/B value" as you mention, the easiest option would be to use a UISwitch or -in the general case of possibly more than 2 options- a UISegmentedControl (as @rmaddy suggested in the question's comments) . 
These controls have built-in the "choose just one out of many" functionality that you are looking for.
The drawbacks of the switch are:
If you still want two separate UIButton instances, you can:
Have references to both buttons in your view controller (@IBOutlets wired using Interface Builder), e.g.:
@IBOutlet weak var leftButton: UIButton!
@IBOutlet weak var rightButton: UIButton!
Implement the action method for both buttons in such a way that it sets the selected state of the tapped button, and resets the other one. For example:
@IBAction func buttonAction(sender: UIButton) {
    if sender == leftButton {
        leftButton.isSelected = true
        rightButton.isSelected = false
    } else if sender == rightButton{
        leftButton.isSelected = false
        rightButton.isSelected = true
    }
}
This is a quick-and-dirty solution for just two buttons. If you want a generic radio group of n-buttons, there are open source solutions on GitHub, etc...
Try this.
First create both button separate @IBOutlet.
@IBOutlet weak var btnYes: UIButton!
@IBOutlet weak var btnNo: UIButton!
Set Both Button Tag Like this and you also set tag using storyboard.
override func viewDidLoad() {
      super.viewDidLoad()
    btnYes.tag = 1
    btnNo.tag = 2
}
Implement Common @IBAction method for both buttons
@IBAction func btnYesNoTapped(_ sender: UIButton) {
        if sender.tag == 1 {
            self.IsBtnSelected(isSelect: true, with: self.btnYes)
        }else {
            self.IsBtnSelected(isSelect: true, with: self.btnNo)
        }
}
Create Custome Method
func IsBtnSelected(isSelect:Bool,with sender:UIButton){
        self.btnYes.isSelected = false
        self.btnNo.isSelected = false
        sender.isSelected = isSelect
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With