-(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];
}
}
}
初始化tableView
- (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];
}
这次它甚至没有将背景图像从option.jpg更改为绿色/红色。所有点击它总是选项。
* *和我的代码......我没有得到任何错误...... * *我测试了断点它正确地通过所有声明......甚至图像的变化..但我看不到任何背景图像的变化。告诉我接下来的事情......
-(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];
}
}
所有的逻辑都没问。即使我正在计算得分......我测试了......只是这个图像问题不起作用....请给我一个方法.....我是全新的iphone ....学习它..如果有任何错误或不好的做法,请给我适当的指导...
按钮aciton的代码:
-(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;
}
tableView Indexpath中的代码:
- (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;
}
答案 0 :(得分:0)
我仍然不完全理解你的问题..如果你想要一个普通的按钮图像,另一个当用户触摸按钮时,你可以在创建按钮时直接给它们..不需要在按钮时更改图像点击..
[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];
由于您使用了按钮的突出显示状态,我认为只有当用户手上的按钮时才想要绿色图像(即当用户释放按键时更改为正常图像..)。在这种情况下,上面的代码将起作用。
但是如果你希望按钮图像在用户发布后保持绿色,那么你应该使用UIButton的selected(UIControlStateSelected)属性..
编辑:看到评论后..
在这种情况下,当按钮alloc和init只给出正常状态图像..
[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;
}
第二次修改:看到编辑后的问题
首先,不要比较像这样的两个字符串..
if(str==[dictionary objectForKey:@"option1"])
而是使用compare()函数或isEqualToString()函数进行比较..
第二件事,你在btnAction函数中使用“ cell ”。我不能相信它在编译时没有显示“ cell is unclared ”错误。 第三,纠正以上两个错误。 第四,纠正错误后尝试使用断点调试代码。你已经试过了吗?在btnAction函数中放置一个断点,看看它是否正在按照你想要的方式执行。然后用你的观察更新这个问题。目前,你的代码中有很多错误,所以我不知道哪一个导致了什么..对我来说,这段代码根本不应该编译..
第三编辑:只是一个事后的想法 试试这个..
-(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;
}
}
第四次编辑:还有一次事后的想法
-(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];
}
}
}