0

我正在使用带有 xib backgroundImage 的UIButton是check_box_off.png,同时单击 atmbtn backgroundImage 更改为check_box_on.png同时NSLog打印yesString并再次单击 atmbtn 没有更改 backgroundImage 相同check_box_on.png同时 NSLog 打印相同yesString仅.. 和再次叮当atmbutton同样只发生..

- (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 打印:

自动取款机=是

自动取款机=是

自动取款机=是

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

4

5 回答 5

1

用这个:

[atmbtn setBackgroundImage:[UIImage imageNamed:@"check_box_on.png"] forState:UIControlStateNormal];
于 2012-10-31T10:04:47.843 回答
0

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

.h 文件

BOOL _isClicked;

.m 文件

-(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;
     }
}
于 2012-10-31T10:02:50.663 回答
0

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

尝试

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

代替

if([atmbtn backgroundImageForState:UIControlStateNormal] ==
   [UIImage imageNamed:@"check_box_on.png"])
于 2012-10-31T10:03:49.323 回答
0

你可以简单地使用

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

atmbtn您的xib中按钮的出口在哪里。

于 2012-10-31T10:05:40.010 回答
0

嗨尝试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);
    }
}
于 2012-10-31T10:06:45.393 回答