Ios 如何清除UIButton背景图像状态?

Ios 如何清除UIButton背景图像状态?,ios,ios4,uiimage,Ios,Ios4,Uiimage,在tableView的初始化中 -(void) btnAction:(id) sender { NSString *str =((UIButton*)sender).titleLabel.text; NSLog(@"%@",str); NSLog(@"%@",[dictionary objectForKey:@"option3"]); correctAns=[dictionary objectForKey:@"answer"]; if(st

在tableView的初始化中

-(void) btnAction:(id) sender {

    NSString *str =((UIButton*)sender).titleLabel.text;

    NSLog(@"%@",str);
    NSLog(@"%@",[dictionary objectForKey:@"option3"]);

    correctAns=[dictionary objectForKey:@"answer"];

        if(str==[dictionary objectForKey:@"option1"])
{

    selectedAns=@"1";

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    {
     NSLog(@"this is correct");
    [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];

     countCorrect++;
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {

        [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

}
}
更改代码时: 这一次,它甚至没有将背景图像从option.jpg更改为绿色/红色。在所有单击中始终是option.jpg

**我的代码……我没有收到任何错误。。。 **我使用断点测试,它正确地处理了所有语句…甚至图像的更改..但我看不到任何背景图像的更改。告诉我下一步是什么

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"BeginingCell";

    cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {

        NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];

        for(id CurrentObject in topLevelObjects)
        {
            if ([CurrentObject isKindOfClass:[BeginingCell class]]) {

                cell=(BeginingCell *) CurrentObject;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;


                break;
            }
        }

    }


    if(indexPath.row==0)
    {


       imageURL=[dictionary objectForKey:@"questionImage"];
     //  imageName=[dictionary objectForKey:@"questionImage"];
     // imageURL=@"http://localhost:85/2/";
     // imageURL=[imageURL stringByAppendingString:imageName];

        NSURL *url = [NSURL URLWithString:imageURL];
        NSData *data = [NSData dataWithContentsOfURL:(NSURL *)url];
        UIImage *img = [[UIImage alloc]  initWithData:data];



        cell.lblScoreCurrent.text=[NSString stringWithFormat:@"%d", reloader];

        cell.lblScore.text=@"/30";
        cell.myImageView.image = img;
        cell.SectionTitle.text=[dictionary objectForKey:@"question"];




        [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
        [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateSelected];
        [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState: UIControlStateSelected];
}
所有的逻辑都是好的。甚至我正在计算分数。我测试了它…只是这个图像问题不起作用…请告诉我一个方法…我对iphone是全新的…学习它…所以如果有任何错误或不好的做法,请给我适当的指导

使用代码后: 按钮aciton的代码:

  -(void) btnAction:(id) sender {

    NSString *str =((UIButton*)sender).titleLabel.text;

NSLog(@"%@",str);
NSLog(@"%@",[dictionary objectForKey:@"option3"]);

correctAns=[dictionary objectForKey:@"answer"];

if([str isEqualToString:([dictionary objectForKey:@"option1"])])

{

    selectedAns=@"1";

    if ([selectedAns isEqualToString:correctAns]) 
    {
     NSLog(@"this is correct");
     [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];

     countCorrect++;
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {

        [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

}
tableView中的代码:

    -(void) btnAction:(id) sender {

((UIButton *)sender).selected=FALSE;
NSString *str =((UIButton*)sender).titleLabel.text;
correctAns=[dictionary objectForKey:@"answer"];

if([str isEqualToString:([dictionary objectForKey:@"option1"])])

{

    selectedAns=@"1";
    NSLog(@"Selected Ans = %@",selectedAns);
    NSLog(@"Correct = %@",correctAns);


    if ([selectedAns isEqualToString:correctAns]) 
    {
     NSLog(@"this is correct");

     [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];

    //  sleep(3);

     countCorrect++;
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {

        [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];



        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    ((UIButton*)sender).selected = true;

}

}

我仍然不完全理解您的问题。如果您希望按钮有一个普通图像,用户触摸按钮时有另一个图像,您可以在创建按钮时直接提供。单击按钮时无需更改图像

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"BeginingCell";

cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) {

    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];

    for(id CurrentObject in topLevelObjects)
    {
        if ([CurrentObject isKindOfClass:[BeginingCell class]]) {

            cell=(BeginingCell *) CurrentObject;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;


            break;
        }
    }

}


if(indexPath.row==0)
{


   imageURL=[dictionary objectForKey:@"questionImage"];
 //  imageName=[dictionary objectForKey:@"questionImage"];
 // imageURL=@"http://localhost:85/2/";
 // imageURL=[imageURL stringByAppendingString:imageName];

    NSURL *url = [NSURL URLWithString:imageURL];
    NSData *data = [NSData dataWithContentsOfURL:(NSURL *)url];
    UIImage *img = [[UIImage alloc]  initWithData:data];



    cell.lblScoreCurrent.text=[NSString stringWithFormat:@"%d", reloader];

    cell.lblScore.text=@"/30";
    cell.myImageView.image = img;
    cell.SectionTitle.text=[dictionary objectForKey:@"question"];


    [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
    [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
    [cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
    [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];

    cell.ansBtn1.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.ansBtn2.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.ansBtn3.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.ansBtn4.titleLabel.lineBreakMode = UILineBreakModeWordWrap;



    cell.ansBtn1.titleLabel.textAlignment = UITextAlignmentCenter;
    cell.ansBtn2.titleLabel.textAlignment = UITextAlignmentCenter;
    cell.ansBtn3.titleLabel.textAlignment = UITextAlignmentCenter;
    cell.ansBtn4.titleLabel.textAlignment = UITextAlignmentCenter;


    [cell.ansBtn1 setTitle:[dictionary objectForKey:@"option1"] forState:UIControlStateNormal];
    [cell.ansBtn2 setTitle:[dictionary objectForKey:@"option2"] forState:UIControlStateNormal];
    [cell.ansBtn3 setTitle:[dictionary objectForKey:@"option3"] forState:UIControlStateNormal];
    [cell.ansBtn4 setTitle:[dictionary objectForKey:@"option4"] forState:UIControlStateNormal];


    [cell.ansBtn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.ansBtn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.ansBtn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.ansBtn4 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];

    [cell.ansBtn1 addTarget:self action:@selector(btnActionUp:) forControlEvents:UIControlEventTouchDown];

    //cell.ansBtn1.showsTouchWhenHighlighted=TRUE;
    cell.ansBtn2.showsTouchWhenHighlighted=TRUE;
    cell.ansBtn3.showsTouchWhenHighlighted=TRUE;
    cell.ansBtn4.showsTouchWhenHighlighted=TRUE;



}


return cell;
由于您使用了按钮的突出显示状态,我假定您只希望在用户手按按钮时使用绿色图像(即当用户释放键时更改为正常图像)。在这种情况下,上述代码将起作用

但如果您希望按钮图像即使在用户释放后仍保持绿色,则应使用UIButton的selected(UIControlStateSelected)属性

编辑:看到评论后。

在这种情况下,当按钮alloc和init仅给出正常状态映像时

[cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
[cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateHighlighted];
[cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
[cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateHighlighted];
[cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
[cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateHighlighted];
[cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option.png"] forState: UIControlStateNormal];
[cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState: UIControlStateHighlighted];
然后点击按钮

[cell.ansBtn setBackgroundImage:[UIImage imageNamed:@"option.png" forState:UIControlStateNormal]];
第二次编辑:在看到编辑的问题后

首先,千万不要在这样的两个字符串之间进行比较

-(void)optionButtonClicked:(UIButton*)sender{
   if(correct){
     // green image..
     [sender setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
   }else{//red image
     [sender setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
   }
   sender.selected = true;
}
而是使用compare()函数或isEqualToString()函数进行比较

第二件事,您正在btnAction函数中使用“单元格”。我不敢相信它在编译时没有显示“单元格未声明””错误。 第三,纠正以上两个错误。 第四,更正错误后,尝试使用断点调试代码。你已经试过了吗?在btnAction函数中放置一个断点,看看它是否以您想要的方式执行..然后用您的观察结果更新此问题..目前,您的代码中有太多错误,以至于我无法判断是哪个错误导致了什么..对我来说,此代码根本不应该编译

第三次编辑:只是事后想 试试这个

 if(str==[dictionary objectForKey:@"option1"])
第四次编辑:再考虑一次

-(void) btnAction:(id) sender {
   ((UIButton*)sender).selected = FALSE; 
    NSString *str =((UIButton*)sender).titleLabel.text;
    NSLog(@"%@",str);
    NSLog(@"%@",[dictionary objectForKey:@"option3"]);
    correctAns=[dictionary objectForKey:@"answer"];
    if([str isEqualToString:([dictionary objectForKey:@"option1"])]){
       selectedAns=@"1";
       If ([selectedAns compare:correctAns]==NSOrderedSame) {
          NSLog(@"this is correct");
          [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateSelected];
          countCorrect++;
        //  cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect]; 

        }else {
           [((UIButton*)sender) setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
           cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
         }
         ((UIButton*)sender).selected = true;
    }
}

实际上我有3张背景图片…option.png(正常),option_green.png(正确),option_red.png(不正确)…这就像是一个测验应用程序。如果我单击正确的ans按钮,将高亮显示绿色图像。如果错误的按钮将高亮显示reg图像。你明白了吗?或者我应该上传更多的代码?请查看更新的代码…我使用了你的逻辑…查看bakground初始化的tableView代码。以及更改…问题在第一次单击时没有更改…在第二次单击时,它将更改为绿色/红色,与第一次单击时一样…因此,我在当前单击中获得了上一次单击的更改…我是否做错了?编辑问题时,决不要删除原始问题..只需将新编辑添加到原始问题中即可问题请现在看一看…并告诉我我应该回到原来的问题吗?我的意思是如果你需要..或者暂时不提..告诉我现在应该做什么..它不会随着代码更改…((UIButton*)发件人)。selected=true;你在编辑的问题中忘记了这一行。你在调试时忘记了吗?这是更新的代码作为你的说明。我调试了它。但是没有问题。我在下一页中得到了reg/green图像。你有问题吗?
-(void) btnAction:(id) sender {
    NSString *str =((UIButton*)sender).titleLabel.text;
    NSLog(@"%@",str);
    NSLog(@"%@",[dictionary objectForKey:@"option3"]);
    correctAns=[dictionary objectForKey:@"answer"];
    if([str isEqualToString:([dictionary objectForKey:@"option1"])]){
       selectedAns=@"1";
       If ([selectedAns compare:correctAns]==NSOrderedSame) {
          NSLog(@"this is correct");
          [((UIButton*)sender) setImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateNormal];
          countCorrect++;
        //  cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect]; 

        }else {
           [((UIButton*)sender) setImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateNormal];
           cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];
         }
    }
}