title: How to clear UIButton background image State?
body: <pre><code>-(void) btnAction:(id) sender {
  
      NSString *str =((UIButton*)sender).titleLabel.text;
  
      NSLog(@"%@",str);
      NSLog(@"%@",[dictionary objectForKey:@"option3"]);
  
      correctAns=[dictionary objectForKey:@"answer"];
  
          if(str==[dictionary objectForKey:@"option1"])
  {
  
      selectedAns=@"1";
  
      if ([selectedAns compare:correctAns]==NSOrderedSame) 
      {
       NSLog(@"this is correct");
      [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
  
       countCorrect++;
       cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
      else 
      {
  
          [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
          cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
  }
  }
  </code></pre>
  
  <p>in Initialization of tableView </p>
  
  <pre><code>        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
      static NSString *CellIdentifier = @"BeginingCell";
  
      cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
  
      if (cell == nil) {
  
          NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];
  
          for(id CurrentObject in topLevelObjects)
          {
              if ([CurrentObject isKindOfClass:[BeginingCell class]]) {
  
                  cell=(BeginingCell *) CurrentObject;
                  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
  
                  break;
              }
          }
  
      }
  
  
      if(indexPath.row==0)
      {
  
  
         imageURL=[dictionary objectForKey:@"questionImage"];
       //  imageName=[dictionary objectForKey:@"questionImage"];
       // imageURL=@"http://localhost:85/2/";
       // imageURL=[imageURL stringByAppendingString:imageName];
  
          NSURL *url = [NSURL URLWithString:imageURL];
          NSData *data = [NSData dataWithContentsOfURL:(NSURL *)url];
          UIImage *img = [[UIImage alloc]  initWithData:data];
  
  
  
          cell.lblScoreCurrent.text=[NSString stringWithFormat:@"%d", reloader];
  
          cell.lblScore.text=@"/30";
          cell.myImageView.image = img;
          cell.SectionTitle.text=[dictionary objectForKey:@"question"];
  
  
  
  
          [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
          [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateSelected];
          [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState: UIControlStateSelected];
  }
  </code></pre>
  
  <h2>With changes with your code:</h2>
  
  <p>This time it's not even changing the background image from option.jpg to green/red.It's always option.jpg in all click.</p>
  
  <p>*<strong>*And with my code...i didn't get any error...
  *</strong>*I tested with break point it's going through all the statement properly...even the change of image..but i can't see any backgroud image change.Tell me what is next...</p>
  
  <pre><code>  -(void) btnAction:(id) sender {
  
      NSString *str =((UIButton*)sender).titleLabel.text;
  
  NSLog(@"%@",str);
  NSLog(@"%@",[dictionary objectForKey:@"option3"]);
  
  correctAns=[dictionary objectForKey:@"answer"];
  
  if([str isEqualToString:([dictionary objectForKey:@"option1"])])
  
  {
  
      selectedAns=@"1";
  
      if ([selectedAns isEqualToString:correctAns]) 
      {
       NSLog(@"this is correct");
       [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
  
       countCorrect++;
       cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
      else 
      {
  
          [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
          cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
  }
  </code></pre>
  
  <p>All the logic are ok..even i am calculating the score..i tested it...just this image issue is not working....please show me a way.....i am totally new in iphone....learning it..so please give me proper instruction if anything is wrong or bad practice...</p>
  
  <h2>After using your code :</h2>
  
  <p>Code for the button aciton :</p>
  
  <pre><code>    -(void) btnAction:(id) sender {
  
  ((UIButton *)sender).selected=FALSE;
  NSString *str =((UIButton*)sender).titleLabel.text;
  correctAns=[dictionary objectForKey:@"answer"];
  
  if([str isEqualToString:([dictionary objectForKey:@"option1"])])
  
  {
  
      selectedAns=@"1";
      NSLog(@"Selected Ans = %@",selectedAns);
      NSLog(@"Correct = %@",correctAns);
  
  
      if ([selectedAns isEqualToString:correctAns]) 
      {
       NSLog(@"this is correct");
  
       [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
  
      //  sleep(3);
  
       countCorrect++;
       cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
      else 
      {
  
          [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
  
  
  
          cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
  
      }
  
      ((UIButton*)sender).selected = true;
  
  }
  </code></pre>
  
  <p>code in the tableView Indexpath :   </p>
  
  <pre><code>  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  static NSString *CellIdentifier = @"BeginingCell";
  
  cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  
  
  if (cell == nil) {
  
      NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];
  
      for(id CurrentObject in topLevelObjects)
      {
          if ([CurrentObject isKindOfClass:[BeginingCell class]]) {
  
              cell=(BeginingCell *) CurrentObject;
              cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
  
              break;
          }
      }
  
  }
  
  
  if(indexPath.row==0)
  {
  
  
     imageURL=[dictionary objectForKey:@"questionImage"];
   //  imageName=[dictionary objectForKey:@"questionImage"];
   // imageURL=@"http://localhost:85/2/";
   // imageURL=[imageURL stringByAppendingString:imageName];
  
      NSURL *url = [NSURL URLWithString:imageURL];
      NSData *data = [NSData dataWithContentsOfURL:(NSURL *)url];
      UIImage *img = [[UIImage alloc]  initWithData:data];
  
  
  
      cell.lblScoreCurrent.text=[NSString stringWithFormat:@"%d", reloader];
  
      cell.lblScore.text=@"/30";
      cell.myImageView.image = img;
      cell.SectionTitle.text=[dictionary objectForKey:@"question"];
  
  
      [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
      [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
      [cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
      [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
  
      cell.ansBtn1.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      cell.ansBtn2.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      cell.ansBtn3.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      cell.ansBtn4.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
  
  
  
      cell.ansBtn1.titleLabel.textAlignment = UITextAlignmentCenter;
      cell.ansBtn2.titleLabel.textAlignment = UITextAlignmentCenter;
      cell.ansBtn3.titleLabel.textAlignment = UITextAlignmentCenter;
      cell.ansBtn4.titleLabel.textAlignment = UITextAlignmentCenter;
  
  
      [cell.ansBtn1 setTitle:[dictionary objectForKey:@"option1"] forState:UIControlStateNormal];
      [cell.ansBtn2 setTitle:[dictionary objectForKey:@"option2"] forState:UIControlStateNormal];
      [cell.ansBtn3 setTitle:[dictionary objectForKey:@"option3"] forState:UIControlStateNormal];
      [cell.ansBtn4 setTitle:[dictionary objectForKey:@"option4"] forState:UIControlStateNormal];
  
  
      [cell.ansBtn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
      [cell.ansBtn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
      [cell.ansBtn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
      [cell.ansBtn4 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
  
      [cell.ansBtn1 addTarget:self action:@selector(btnActionUp:) forControlEvents:UIControlEventTouchDown];
  
      //cell.ansBtn1.showsTouchWhenHighlighted=TRUE;
      cell.ansBtn2.showsTouchWhenHighlighted=TRUE;
      cell.ansBtn3.showsTouchWhenHighlighted=TRUE;
      cell.ansBtn4.showsTouchWhenHighlighted=TRUE;
  
  
  
  }
  
  
  return cell;
  </code></pre>
  
  <p>}</p>