title: Selecting and deselecting collection of buttons
body: <p>I have three buttons named <code>One, Two,</code> and <code>Three,</code> and a function <code>buttonPressed</code> for collection of those three buttons as follows.</p>
  
  <pre><code>var btnTag = [Int]()
  
  @IBAction func buttonPressed(_ sender: UIButton) {
  
      guard let button = sender as UIButton? else { return }
      if(btnTag.contains((sender as AnyObject).tag!))
      {
          if let index = btnTag.index(of: (sender as AnyObject).tag!)
          {
              btnTag.remove(at: index)
          }
      }
      else
      {
         btnTag.append((sender as AnyObject).tag!)
      }
  
      if !button.isSelected {
          button.isSelected = true
          button.setTitleColor(.red, for: .normal)
      }
      else
      {
          button.isSelected = false
          button.setTitleColor(.white, for: .normal)
      }
  }
  </code></pre>
  
  <p>I like to change color of button as <code>red</code> when clicked and then when I click other button the previous button color as <code>white</code>. So if I press <code>One,</code> I want <code>One</code> to be <code>red</code> and then when I pres <code>Two</code>, I want <code>Two</code> to be <code>red</code> and <code>One</code> as <code>white</code>. I did the above but it is allowing <code>multiple selection</code> and not able to <code>deselect</code> previous changes. How do I solve this?</p>