Swift create custom logic for radio button

Viewed 4578

I have four custom UIButton, I applied button image radio (checked and checked). four button I have separate action method I can change the Image easily but if I checked first button another three button need uncheck. it should react like radio button.

Here, below my code

 @IBAction func first_scheme(_ sender: Any) {

        bRec = !bRec
        if (bRec == true) {
            firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
        } else {

            firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
        }
    }
3 Answers

If you have 4 radio buttons at all times, you can put them in an array.

var radioButtons: [ButtonType] = [r1, r2, r3, r4]

You can now access the button in a loop and set the values for the other button to 'unchecked'.

func setRadioButtons(button: ButtonType) {
    for radioButton in radioButtons {
        if radioButton !== button {
            radioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
        }
    }   
}

@IBAction func first_scheme(_ sender: Any) {
    bRec = !bRec
    if bRec {
        firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
    } else {
        firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
    }
    setRadioButtons(button: sender)
}

alternate method

If all you want to do is check the button clicked and uncheck the other buttons, the logic is simpler.

Create the common action for all radio buttons as well create the IBOutletcollection for your all UIButtons ,

var radioButtons: [UIButton] = [r1, r2, r3, r4]

finally execute the common method as

func setRadioButtons(button: UIButton) {
    for getradioButton in radioButtons {
        getradioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
    }
    button.setImage(UIImage(named: "check.png"), for: .normal)   
}

I suggest you set tags on the buttons with the simple tag property, and then you save it from a generic listener.

//first button selected
var lastTag = 0
@IBAction func first_scheme(_ sender: UIButton) {
    buttonArray[lastTag].setImage(UIImage(named: "uncheck.png"), for: .normal)
    lastTag = sender.tag
    sender.setImage(UIImage(named: "check.png"), for: .normal)
}

You Can override isSelected variable in the custom UIButton and set the image depend on the isSelected value.

class customButton: UIButton {

    override var isSelected: Bool {

        willSet {

           self.setImage(UIImage.init(named: "checked"), for: .normal)
        }

        didSet {
            self.setImage(UIImage.init(named: "unchecked"), for: .normal)
        }
    }
}

After making 4 IBOutlets and 4 IBActions for the four custom UIButtons. you can easily select and unselect the buttons and apply your custom behaviour.

@IBAction func firstButtonAction(_ sender: Any) {

    if let button = sender as? customButton {
        button.isSelected = !button.isSelected
    }

    secondButton.isSelected = false
    thirdButton.isSelected = false
    fourthButton.isSelected = false
}
Related