0

I have 30 buttons with 3 on each row. I want the user to be able to select one of three per row, with the left column being default ("ee"). I can't seem to make this work...

With the following code I am able to change the color of buttons when they are clicked, but it doesn't deselect upon clicking another button. I want to send the button title string to the "ext" variable, which will tell the app which option the user selected on each row (each row has a different variable)

I am using this code for one row of 3 buttons:

@IBAction func `extension`(_ sender: UIButton) {
        ext = String(sender.titleLabel!.text ?? "ee")
        sender.backgroundColor = #colorLiteral(red: 0.09334511863, green: 0.1549694077, blue: 0.2597150259, alpha: 1)
        sender.isSelected = !sender.isSelected
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
imeyer
  • 19
  • 6
  • You want a radio button behavior for each row. Read here: https://stackoverflow.com/a/29117898/13087977 – Deitsch Jun 03 '20 at 08:09
  • Do I make a new array for each row? It seems like a lot of code would be required to make it work for this many buttons. I was on a Swift course where they made an app with three buttons using an IBOutlet for each button. I am trying to pick apart that code and see if I can make it work – imeyer Jun 03 '20 at 16:55
  • For 30 Buttons you gonna need 30 Outlets. One way or the other you will need that. In the post i sent earlier he just groups 3 buttons and the logic is in the RadioButtonController. You need 10 arrays to do that, but you only code the logic once - which is great! Doing it straight forward with IBActions would cause a lot of duplicate/similar code. – Deitsch Jun 04 '20 at 07:57
  • Ill give it a go! Im really new at this, still trying to wrap my head around how to put in these radio buttons.. – imeyer Jun 04 '20 at 20:48

1 Answers1

0

I ended up just adding this to each row, starting with the button names

    aano.isSelected = false
    aama.isSelected = false
    aaye.isSelected = false
    sender.isSelected = true
imeyer
  • 19
  • 6