2011-04-28 11 views
0

일부 컨트롤을 보여주는 tableView가 있습니다. UIButton.i에서 올바른 ans에 대한 옵션과 마찬가지로 올바른 배경으로 버튼 배경 이미지를 변경하고 click 이벤트에서 잘못된 ans를 변경할 수 있습니다. 어떤 사람이 내게 아이디어를 줄 수 있습니까? 내가 같은 배경 이미지 변경하려고 :UIButton 배경 이미지 클릭 변경시

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

과 내가 같은 backgound에 설정 한 :

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

} 

문제가 여기

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

} 

을 그리고 클릭 이벤트입니다 나는 배경을 변경하지 처음 버튼을 클릭하고 있습니다.하지만 다음 클릭에서 변경 논리 (녹색/빨강 변경 뜻)에 따라 변화를 받고 있는데 ... 내가 어떻게해야합니까 그의 케이스. 해결책은 무엇입니까?

+0

또한 compare :를 사용하여 문자열을 비교하십시오 – Robin

+0

예 i kno that ... –

+0

== 연산자로 두 문자열을 비교하는 것이 잘못된 이유는 무엇입니까? –

답변

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: 당신은 또한 버튼의 제목 라벨로 ANS을 보여 주어야 원인

편집
대신

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

의 당신은

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

를 사용해야합니다.

+0

[cell.ansBtn1 setTitle : [dictionary objectForKey : @"option1 "] forState : UIControlStateNormal]을 (를) 사용하고 있습니다. ans를 sgowing하기 위해서 ... ok 나는 당신의 ans을 보아요. –

+0

나는이 링크들에서 더 좋은 아이디어를 얻지 못했습니다 ... –

+0

사용자가 버튼을 눌렀을 때 버튼의 배경 이미지를 설정할 필요가 있습니다. . – Robin

0

UIbutton에 대상 메서드를 추가하십시오. 정의 - (IBAction) btnClick : (id) 보낸 사람. 그런 다음이 메서드에서 보낸 사람 개체에서 단추를 만들면 단추를 클릭 할 때마다 단추의 setBackgroundImage 메서드를 사용하여 보낸 사람 개체를 가져와 배경 이미지를 변경합니다. 이것은 당신을 도울 수 있습니다.