2011-04-28 167 views
0

我有一个tableView显示一些控件。在UIButton.I中正确的ans的选项想要改变正确ans中的按钮背景图像和单击event.how中的错误ans。这样做吗?任何人都可以给我一个想法吗? 我试图更改背景图片为:UIButton背景图像点击更改

[cell.ansBtn2 setImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected]; 

,我已经设置backgound为:

- (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"]; 

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



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

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




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



    } 


    return cell; 

} 

这里是Click事件:

 -(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"]) 
{ 
    [email protected]"1"; 

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    { 

    [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted]; 
    NSLog(@"this is correct"); 

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


    } 

    else 
    { 

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

    } 

} 

else if(str==[dictionary objectForKey:@"option2"]) 
{ 
    [email protected]"2"; 
    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    { 
     [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted]; 
     NSLog(@"this is correct"); 
     countCorrect++; 
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect]; 

    } 

    else 
    { 
     [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted]; 
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect]; 

    } 
} 


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

    [email protected]"3"; 
    NSLog(@"selected ans is %@",selectedAns); 
    NSLog(@"correct ans is %@",correctAns); 

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

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

    } 

    else 
    { 
     [cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted]; 
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect]; 

    } 

} 

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

    [email protected]"4"; 
    NSLog(@"selected ans is %@",selectedAns); 
    NSLog(@"correct ans is %@",correctAns); 

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    { 

     [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted]; 

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

    } 

    else 
    { 
     [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted]; 
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];    

    } 

} 


else { 
    NSLog(@"ERROR"); 
} 


reloader++; 

if (reloader>=[QuestionMutableArray count]) { 



    NSString *str=[self GetSkill]; 
    NSString *totalTime=[NSString stringWithFormat:@"%i . %i minutes",7-timeMin,60-timeSecond]; 
    NSString *str2=[NSString stringWithFormat:@"Game Over :\n Knowlwdge Level : %@ \n Total Correct Answer :%i \n Total Time : %@ \n Total Question Answered : %i \n",str,countCorrect,totalTime,reloader]; 

    appAlert=[[UIAlertView alloc]initWithTitle:@"Game Over :\n" message:str2 
               delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil]; 
    [appAlert addButtonWithTitle:@"PlayAgain"]; 



    UIImage *alertBoxImage = [UIImage imageNamed:@"first_look.jpg"]; 

    UIImageView *_backgroundImageView = [[UIImageView alloc] initWithImage:alertBoxImage]; 

    _backgroundImageView.frame = CGRectMake(0, 0, 282, 220); 

    _backgroundImageView.contentMode = UIViewContentModeScaleToFill; 

    [appAlert addSubview:_backgroundImageView]; 

    [appAlert sendSubviewToBack:_backgroundImageView]; 


    [appAlert show];   
    [dictionary release]; 
    [QuestionMutableArray release]; 
    countCorrect=0; 


} 
else { 

    dictionary=[QuestionMutableArray objectAtIndex:reloader]; 
    [self.tableView reloadData]; 
} 

} 

问题是,当我点击一个按钮,第一次背景没有变化,但在接下来的点击中,我正在根据变化逻辑集(我的意思是绿色/红色变化)得到变化......我应该做什么t他的案例。解决方案是什么?

+0

并且还使用比较:比较字符串 – Robin 2011-04-28 08:44:06

+0

是我知道那个... – 2011-04-28 08:51:51

+0

将两个字符串与==运算符进行比较会出现什么问题? – 2011-04-28 09:01:46

回答

0

更多编辑:
不要使用==以检查字符串是否相等,而是则应使用isEqualToString:或比较:函数

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

你可以做

if([string1 compare:string2] == NSOrderedSame) 
{ 
    //Do something... 
} 

因为==将检查2个指针地址在内存中的位置,而不是这些指针的内容。

OLD

您可以使用

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setBackgroundImage:forState

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setImage:forState

如果你是显示ANS作为按钮的标题标签,那么你有使用setBackgroundImage:forState:

编辑的
代替

[cell.ansBtn2 setImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected]; 

你应该使用

[cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected]; 

,因为你还可以选择显示您的ANS作为按钮的标题标签。

+0

我正在使用[cell.ansBtn1 setTitle:[dictionary objectForKey:@”option1“] forState:UIControlStateNormal];为sgowing回答...好吧,我正在看看你的ans – 2011-04-28 05:13:47

+0

我没有得到任何更好的主意,在这些链接... – 2011-04-28 06:11:59

+0

你需要设置按钮的背景图像时,用户按下一个按钮多数民众赞成在它。 – Robin 2011-04-28 06:13:54

0

在您的UIbutton中添加目标方法。定义它 - (IBAction)btnClick:(id)发件人。然后在这个方法中从发送者对象创建按钮,所以每当我们点击按钮时,我们将得到发送者对象,并使用setBackgroundImage方法的按钮来改变它的背景图像。这可能会帮助你。