2012-08-06 62 views
4

我这样做是为了改变UIButton图像,这里所有“开关”UIButtonIBOutlets的Objective-C:UIButton的图像不改变

-(IBAction)userselect:(id)sender 
{ 
    UIButton *btnTagcatcher =(UIButton *)sender; 
if(btnTagcatcher.tag == 1) 
    { 
     if(btnimgflag1 == 0) 
      { 
       NSString* path = [[NSBundle mainBundle] pathForResource:@"check-box-withtick-1n1" 
                    ofType:@"png"]; 
       UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; 
       [switch1 setImage:image forState:UIControlStateNormal]; 
       NSString* path1 = [[NSBundle mainBundle] pathForResource:@"check-box-withouttickn1" 
                    ofType:@"png"]; 
       UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:path1]; 
       [switch2 setImage:image1 forState:UIControlStateNormal]; 
       [switch3 setImage:image1 forState:UIControlStateNormal]; 
       [switch4 setImage:image1 forState:UIControlStateNormal]; 
       btnimgflag2 = 0; 
       btnimgflag3 = 0; 
       btnimgflag4 = 0; 


       btnimgflag1 = 1; 
      } 
      else if(btnimgflag1 == 1) 
      { 
     NSString* path = [[NSBundle mainBundle] pathForResource:@"check-box-withouttickn1" 
                    ofType:@"png"]; 
       UIImage *image = [[UIImage alloc] initWithContentsOfFile:path]; 

       //[switch1 setImage: image]; 
       [switch1 setImage:image forState:UIControlStateNormal]; 
       btnimgflag1 = 0; 
      } 

但有些时候(通常先按下时时间)点击时按钮图像不会改变。

我不知道,这是怎么回事,PLZ。在此引导我,thanx提前。

关于萨德。

回答

1

您能否将此条件else if(btnimgflag1 == 1)更改为简单的else声明。因为在if部分中,您将btnimgflag1指定为1 btnimgflag1 = 1;。只是我的猜测,我有完全相同的问题!如果这没有帮助! IBOutlet connections应该是问题!

1

请调试您的代码,并检查您是否获得了正确的按钮标签。我已经完全实现了你所描述的并复制粘贴你的代码,它不会工作,然后我只是将if(btnTagcatcher.tag == 1)更改为if(btnTagcatcher.tag == 0)。现在它正在工作,请尝试验证这一点。

如果发生这种情况,您有问题,请将您的电子邮件ID发给我,我会向您发送完整的代码。