4

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)
        }
    }
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • **Here is working code try it** https://stackoverflow.com/a/51835163/10150796 **another option** https://stackoverflow.com/a/51632267/10150796 – Nikunj Kumbhani Sep 04 '18 at 12:39

3 Answers3

1

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)   
}
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
  • @Anbu.karthik i did not write this logic because i did not know what the OP means by `bRec` and the logic behind it. – Rakesha Shastri Sep 04 '18 at 12:53
  • are you seen this `but if I checked first button another three button need uncheck`, ?'er is the beginner so she taken the global variable for identify the final click object – Anbu.Karthik Sep 04 '18 at 12:54
  • @Anbu.karthik Yea. i'm saying i did not make that assumption because OP is unchecking his button for true? That is all :) – Rakesha Shastri Sep 04 '18 at 12:55
  • var radioButtons = [UIButton]() radioButtons.append(contentsOf: [r1, r2, r3, r4]) update this two lines. Thanks you. Btw How can I get which button is selected within that setRadioButtons function. I mean any values or index like that. –  Sep 05 '18 at 06:20
  • @Soniya you can edit the answer to better reflect the solution. Btw i think both of them work the same :) – Rakesha Shastri Sep 05 '18 at 06:21
1

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)
}
Vollan
  • 1,887
  • 11
  • 26
0

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
}