How can i remove the .isSelected when the user pressed other button

202 Views Asked by At

Im just beginner in Swift Development. I don't know how to remove the .isSelected when the user choose the other button

2

There are 2 best solutions below

0
Ali kazemi On BEST ANSWER

maybe this is useful for you :

if you have several buttons and every one have different action you can follow these level :

1- in attribue inspector for your button choose a tag for example button 1 tag is 1

2- in the code action one button and connect it for each Botton

3- you can access to every button with this one of a code :

  @IBAction func Buttons(_ sender: UIButton) {
    if sender.tag == 1  {
        // do for your button that tag is eaual 1
    }
    if sender.tag == 2 {
        // do for your button that tag is eaual 2
    }
    if sender.tag == 3 {
        // do for your button that tag is eaual 3
    }
    
}
0
Matt2L On

I would create an array of buttons

@IBOutlet var buttonsArray: [UIButton]!

and then assign the same function for all buttons

for button in buttonsArray {
    button.addTarget(self, action: #selector(buttonClicked(_:)), for: .touchUpInside)
}

When the user clicks the button, you can do whatever you want to with clicked button and with other buttons

@objc func buttonClicked(_ sender: UIButton) {
    for button in buttonsArray {
        if (button != sender) {
            //Do what you need with not clicked button
        }
    }
    //Do what you need with clicked button
}