title: Change Button text and Background Color when clicked in IOS
body: <p>I know this question is asked frequently, I have applied many codes but still I'm not getting solution.</p>
  
  <p>I have three radio buttons, when I select <code>radiobutton1</code> it shows show buttons, now i want that when the user click <code>radiobutton1</code> it shows button, the first button is preselected and if the user wants to select other button from option the button background color and text color should change.</p>
  
  <p>I'm trying some code but only first button color is changed it does not change other buttons colors. My code is:</p>
  
  <pre><code>-(void)btnClick:(UIButton *)sender{
  
      switch (sender.tag) {
          case 1:
              if ([rd1 isSelected]==YES ) {
  
                  [rd1 setSelected:YES];
              }
              else{
                  [rd1 setSelected:YES];
  
                  [rd2 setSelected:NO];
  
                  [rd3 setSelected:NO];
  
                  _house.hidden=NO;
  
                  _flat.hidden=NO;
  
                  _upperP.hidden=NO;
  
                  _lowerP.hidden=NO;
  
                  _farmH.hidden=NO;
  
                  _roomB.hidden=NO;
  
                  _pentH.hidden=NO;
  
                  _resident.hidden=YES;
  
                  _commercial.hidden=YES;
  
                  _agricult.hidden=YES;
  
  
                  _industry.hidden=YES;
  
                  _plot.hidden=YES;
  
                  _plotF.hidden=YES;
  
                  _office.hidden=YES;
  
                  _shop.hidden=YES;
  
                  _wareH.hidden=YES;
  
                  _factory.hidden=YES;
  
                  _building.hidden=YES;
  
                  _other.hidden=YES;
  
                  if ([_house isSelected]==YES) {
  
                      House=_house.titleLabel.text;
  
                      NSLog(@"Button is %@",House);
  
                      _flat.enabled=NO;
  
                      _upperP.enabled=NO;
  
                      _lowerP.enabled=NO;
  
                      _farmH.enabled=NO;
  
                      _roomB.enabled=NO;
  
                      _pentH.enabled=NO;
  
                  }
  
                  else if ([_flat isSelected]==YES) {
  
                      _house.enabled=NO;
  
                      _upperP.enabled=NO;
  
                      _lowerP.enabled=NO;
  
                      _farmH.enabled=NO;
  
                      _roomB.enabled=NO;
  
                      _pentH.enabled=NO;
  
  
                  }
  
                  [self BgColor];
  
  
              }
              break;
  
          case 2:
              if ([rd2 isSelected]==YES ) {
  
                  [rd2 setSelected:NO];
              }
              else{
                  [rd2 setSelected:YES];
  
                  [rd1 setSelected:NO];
  
                  [rd3 setSelected:NO];
  
                  _resident.hidden=NO;
  
                  _commercial.hidden=NO;
  
                  _agricult.hidden=NO;
  
                  _industry.hidden=NO;
  
                  _plot.hidden=NO;
  
                  _plotF.hidden=NO;
  
                  _house.hidden=YES;
  
                  _flat.hidden=YES;
  
                  _upperP.hidden=YES;
  
                  _lowerP.hidden=YES;
  
                  _farmH.hidden=YES;
  
                  _roomB.hidden=YES;
  
                  _pentH.hidden=YES;
  
                  _shop.hidden=YES;
  
                  _wareH.hidden=YES;
  
                  _factory.hidden=YES;
  
                  _building.hidden=YES;
  
  
                  _other.hidden=YES;
  
                  _office.hidden=YES;
  
  
  
  
              }
              break;
  
          case 3:
              if ([rd3 isSelected]==YES ) {
  
                  [rd3 setSelected:NO];
              }
              else{
                  [rd3 setSelected:YES];
  
                  [rd2 setSelected:NO];
  
                  [rd1 setSelected:NO];
  
                  _office.hidden=NO;
  
                  _shop.hidden=NO;
  
                  _wareH.hidden=NO;
  
                  _factory.hidden=NO;
  
                  _building.hidden=NO;
  
                  _other.hidden=NO;
  
                  _house.hidden=YES;
  
                  _flat.hidden=YES;
  
                  _upperP.hidden=YES;
  
                  _lowerP.hidden=YES;
  
                  _farmH.hidden=YES;
  
                  _roomB.hidden=YES;
  
                  _pentH.hidden=YES;
                  _resident.hidden=YES;
  
                  _commercial.hidden=YES;
  
                  _agricult.hidden=YES;
  
                  _industry.hidden=YES;
  
                  _plot.hidden=YES;
  
                  _plotF.hidden=YES;
  
              }
              break;
  
          default:
              break;
      }
  
  }
  
  -(void)BgColor{
  
      if ([_house isSelected]==YES) {
  
          [_house setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  
          _house.backgroundColor=[UIColor redColor];
  
      }
      else if (([_flat isSelected]==YES)){
  
          [_flat setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  
          _flat.backgroundColor=[UIColor redColor];
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/OfbPo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OfbPo.jpg" alt="enter image description here"></a></p>