Radio Button Group Swift 3 Xcode 8

10,958

Solution 1

You can connect all your button's IBAction to one single method.

@IBAction func buttonClick(_ sender: UISwitch) { // you're using UISwitch I believe?

}

You should add all the buttons into an array:

// at class level
var buttons: [UISwitch]!

// in viewDidLoad
buttons = [buttonA, buttonB, buttonC]

Then, write the buttonClick method like this:

buttons.forEach { $0.isChecked = false } // uncheck everything
sender.isChecked = true // check the button that is clicked on

Alternatives:

Try using a UITableView. Each row contains one option. When a row is selected, change that row's accessoryType to .checkMark and every other row's to .none.

If you are too lazy, try searching on cocoapods.org and see what other people have made.

Solution 2

Just make a single selector for all three button's touchUpInside event, and set radio_off image for normal state and radio_on image for selected state in your IB, then only you have to connect btnClicked method to all button's touchUpInside event

class ViewController: UIViewController {

@IBOutlet weak var btnFirst:UIButton!

@IBOutlet weak var btnSecond:UIButton!

@IBOutlet weak var btnThird:UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func btnClicked(sender:UIButton){

    let buttonArray = [btnFirst,btnSecond,btnThird]

    buttonArray.forEach{

        $0?.isSelected = false

    }

    sender.isSelected = true

}
Share:
10,958
Chris Herbst
Author by

Chris Herbst

Updated on June 05, 2022

Comments

  • Chris Herbst
    Chris Herbst almost 2 years

    I have searched various sources and could not find a clear and simple solution for creating the equivalent of a radio button group in Swift 3 with Xcode 8.3 for an iOS application.

    For example if I have 3 buttons in one group and only one should be selected at a time. Currently I am implementing this by changing the state of 2 buttons in the group to not selected when the other one is selected and vice versa.

    @IBAction func buttonA(_ sender: Any) {
        buttonB.isChecked = false
        buttonC.isChecked = false
    }
    
    @IBAction func buttonB(_ sender: Any) {
        buttonA.isChecked = false
        buttonC.isChecked = false
    }
    
    @IBAction func buttonC(_ sender: Any) {
        buttonA.isChecked = false
        buttonB.isChecked = false
    }
    

    However I would expect a more efficient way to do this.

    Any help on a more efficient solution will be appreciated.

  • Chris Herbst
    Chris Herbst almost 7 years
    Thank you. I have edited my original question to be clear that I am referring to iOS. Indeed I have mixed up the documentation.
  • Arash Zeinoddini
    Arash Zeinoddini over 2 years
    Thanks for sharing, but forgot add to: button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)