Rameshwar Gupta
Rameshwar Gupta

Reputation: 791

how to change the background image of button everytime when i clicked on that button

Hi I want to make a checkbox field in for that I am using two images checkbox.png and unchecked.png and i want to replace this image every time when user clicks on button. so my question is how to change the background image of button every time when i clicked on that button.

can I do this for that

 if ([_arsa1.currentImage isEqual:@"checkbox.png"])
  {
    [_arsa1.currentImage isEqual:@"checkbox_checked-1.png"];
   }
  else
   {
    [_arsa1.currentImage isEqual:@"checkbox.png"];
   } 

if yes then why this not works and if no then suggest me some other code Thanku

Upvotes: 0

Views: 94

Answers (2)

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

- (void)viewDidLoad
{
  [btn setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"checkbox_checked-1.png"] forState:UIControlStateSelected];


    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(IBAction)btn:(id)sender
{
    if(btn.selected == TRUE)
    {
        btn.selected = FALSE;
    }
    else
    {
        btn.selected = TRUE;
    }
}

Upvotes: 1

Balu
Balu

Reputation: 8460

set two images for two controll states(UINormalState and UISelectedState) and simply set selected YES/NO to that button it'l change automatically.

    [btn setBackgroundImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"checkbox_checked-1.png"] forState:UIControlStateNormal];

-(IBAction)btnAction:(UIButton *)sender{

    sender.selected = !sender.selected;
}

Upvotes: 0

Related Questions