title: Button changing of state programmatically
body: <p>Im really having a problem with my image with button frame, 
  What I want is: 
  1. When it is selected it will have a checkmark
  2. When the view is dismissed then returned back to the previous screen it will still have the check in the image selected 
  3. When I tap the selected image or another image it will remove the checkmark.</p>
  
  <p>I was able to do it but I'm having problem with the setting of image:</p>
  
  <p>What I want is this:</p>
  
  <p><img src="https://i.stack.imgur.com/VXeYe.png" alt="enter image description here"></p>
  
  <pre><code>UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
  [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
  [myButton setEnabled:NO];
  </code></pre>
  
  <p>But this is the function I needed:</p>
  
  <p><img src="https://i.stack.imgur.com/ehipU.png" alt="enter image description here"></p>
  
  <pre><code>UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
  [myButton setImage:bgImage forState:UIControlStateSelected];
  [myButton setSelected:YES];
  </code></pre>
  
  <p>Here is the code below(If there is something wrong with what I'm doing please tell me, Thanks):</p>
  
  <pre><code>- (void)viewDidLoad {
      UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
      [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
      [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
      // Create view
      UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
  
      int row = 0;
      int column = 0;
      for(int i = 0; i &lt; _thumbs.count; ++i) {
  
          UIImage *thumb = [_thumbs objectAtIndex:i];
          myButton = [UIButton buttonWithType:UIButtonTypeCustom];
          myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
          [myButton setImage:thumb forState:UIControlStateNormal];
          [myButton addTarget:self 
                       action:@selector(buttonClicked:) 
             forControlEvents:UIControlEventTouchUpInside];
          myButton.tag = i; 
          NSLog(@"%i",i);
  
          NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
          NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];
  
          if ( selectedImageSlot1 == i){
              [self turnButtonToSelected];
          } 
  
          [view addSubview:myButton];
  
          if (column == 6) {
              column = 0;
              row++;
          } else {
              column++;
          }
  
      }
  
      [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
  }
  
  - (void)turnButtonToSelected
  {       UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
      [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
      [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
      [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
      [myButton setEnabled:NO];
  }
  - (void)highlightButton:(UIButton *)a {
      //[self turnButtonToSelected];    
  }
  - (IBAction)buttonClicked:(id)sender {
      myButton = (UIButton *)sender;
      self.selectedImage = [_images objectAtIndex:myButton.tag];
  
      if (myButton.selected) {
  
          [sender setSelected:NO];
  
      }else {
  
          UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
          [sender setImage:bgImage forState:UIControlStateSelected];
          [sender setSelected:YES];
      }
  
  
      NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
      [prefs setInteger:myButton.tag forKey:@"slot1"];
  
      [[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected1"];
  
      [[NSUserDefaults standardUserDefaults] synchronize];
  
  
      [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
      [self dismissModalViewControllerAnimated:NO];
  
  }
  </code></pre>
  
  <p>Or is there a way to forcefully make a button enabled whenever it is disabled when tapped also?</p>