Ios 如何在单击同一UIButton时更改UIButton背景图像?

Ios 如何在单击同一UIButton时更改UIButton背景图像?,ios,objective-c,iphone,uibutton,Ios,Objective C,Iphone,Uibutton,我正在使用带有xib背景的ui按钮,单击 atmbtn背景图像更改为.png上的复选框同时NSLog打印是字符串,再次单击atmbtn,背景图像相同在.png上的复选框同时NSLog只打印相同的是字符串。。一次又一次地敲响阿坦布顿的钟声,同样的事情只会发生 - (IBAction)atmAction:(id)sender{ if( [atmbtn backgroundImageForState:UIControlStateNormal] == [UIImage ima

我正在使用带有xib背景的ui按钮,单击 atmbtn背景图像更改为.png上的复选框同时NSLog打印是字符串,再次单击atmbtn,背景图像相同在.png上的复选框同时NSLog只打印相同的是字符串。。一次又一次地敲响阿坦布顿的钟声,同样的事情只会发生

- (IBAction)atmAction:(id)sender{  
    if( [atmbtn backgroundImageForState:UIControlStateNormal] ==
        [UIImage imageNamed:@"check_box_on.png"] ) {
       [atmbtn setImage:[UIImage imageNamed:@"check_box_off.png"] 
               forState:UIControlStateNormal]; 

        noString = @"No"; 
        NSLog(@"atm=%@",noString);
    } else if( [atmbtn backgroundImageForState:UIControlStateNormal] ==
                [UIImage imageNamed:@"check_box_off.png"]) {

        [atmbtn setImage:[UIImage imageNamed:@"check_box_on.png"]
                forState:UIControlStateNormal];

         yesString = @"Yes";
         NSLog(@"atm = %@",yesString);
     }
}
输出:

NSLog打印:

自动取款机=是

自动取款机=是

自动取款机=是


我如何解决这个问题?请分享您的想法。

设置一个BOOL变量,而不是if条件

.h文件

BOOL _isClicked;
-(IBAction)atmAction:(id)sender{  

    if(!isClicked){

       [atmbtn setImage:[UIImage imageNamed:@"check_box_off.png"]
               forState:UIControlStateNormal];   
        _isClicked = YES;      

     } else {

         [atmbtn setImage:[UIImage imageNamed:@"check_box_on.png"]
                 forState:UIControlStateNormal];    
          _isClicked = NO;
     }
}
.m文件

BOOL _isClicked;
-(IBAction)atmAction:(id)sender{  

    if(!isClicked){

       [atmbtn setImage:[UIImage imageNamed:@"check_box_off.png"]
               forState:UIControlStateNormal];   
        _isClicked = YES;      

     } else {

         [atmbtn setImage:[UIImage imageNamed:@"check_box_on.png"]
                 forState:UIControlStateNormal];    
          _isClicked = NO;
     }
}

您正在设置
UIImage
,并且正在检查
backgroundImage

试一试

而不是

if([atmbtn backgroundImageForState:UIControlStateNormal] ==
   [UIImage imageNamed:@"check_box_on.png"])
使用以下命令:

[atmbtn setBackgroundImage:[UIImage imageNamed:@"check_box_on.png"] forState:UIControlStateNormal];
你可以简单地使用

    [atmbtn setBackgroundImage:[UIImage imageNamed:@"check_box_off.png"]
                    forState:UIControlStateNormal];
    [atmbtn setBackgroundImage:[UIImage imageNamed:@"check_box_on.png"]
                    forState:UIControlStateSelected];

其中
atmbtn
是xib中按钮的出口。

请尝试
isEqual
方法,而不是
=
,因为您是在比较对象而不是值。试试这个:

-(IBAction)atmAction:(id)sender{

    if([[atmbtn imageForState:UIControlStateNormal] 
          isEqual:[UIImage imageNamed:@"check_box_on.png"]] ) {

        [atmbtn setImage:[UIImage imageNamed:@"check_box_off.png"]
                forState:UIControlStateNormal];
        noString = @"No";
        NSLog(@"atm = %@",noString);

    } else if( [[atmbtn imageForState:UIControlStateNormal] 
                  isEqual:[UIImage imageNamed:@"check_box_off.png"]] ){

        [atmbtn setImage:[UIImage imageNamed:@"check_box_on.png"]
                forState:UIControlStateNormal];

        yesString = @"Yes";
        NSLog(@"atm = %@",yesString);
    }
}

它工作得很好。。。我设置了check_box_off.png图像作为UIButton背景图像,但我尝试了图像路径。它运行良好。。。我设置了check_box_off.png图像作为UIButton背景图像,但我尝试了图像路径。我使用[atmbtn setBackgroundImage:[UIImage ImageName:@“check_box_off.png”]来表示状态:UIControlStateNormal];而不是[atmbtn setImage:[UIImage IMAGENAME:@“check_box_off.png”]用于状态:UIControlStateNormal];