简体   繁体   中英

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

- (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;
    }
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM