2012-08-03 6 views
1

퀴즈 질문을 무작위로 추출하려고했지만 에서 작동하지 않는 것 같습니다. totalQuestions = nil;에 중단 점을 적용하고 실행했습니다. 디버거는 나에게 보여줍니다퀴즈 응용 프로그램에 대한 무작위 질문

[totalQuestions = (id) 0x00c82733] [0](id)

어떤 실수가 아래 코드에서이 있습니까?

QuizViewController.m 

int totalQuestions = 0, qCount=1, myScore=0; 


// Implement viewDidLoad to do additional setup after loading the view. 


-(void)viewDidLoad { 

    qCount = 1; myScore = 0; 

totalQuestions = [appDelegate.qns count]; 

quizLbl.text =quizLblV; 
headerLbl.text =headerLblV; 

[qBtn setTitle:qBtnV forState:UIControlStateNormal]; 
[qBtnB setTitle:qBtnBV forState:UIControlStateNormal]; 
[qBtnC setTitle:qBtnCV forState:UIControlStateNormal]; 
[qBtnD setTitle:qBtnDV forState:UIControlStateNormal]; 


appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 



id totalQuestions = nil; 
if ([appDelegate.qns count] > 0){ 
    int randomIndex = arc4random()%[appDelegate.qns count]; 
    totalQuestions = [appDelegate.qns objectAtIndex:randomIndex]; 


} 

} 

-(IBAction)buttonWasClicked:(id)sender{ 
UIButton *resultebutton= (UIButton*)sender; 

if (qCount < totalQuestions) { 

    id prevQuestion = [appDelegate.qns objectAtIndex:qCount-1]; 
    NSString * correctAns = [prevQuestion labelAns]; 

    if ([correctAns isEqualToString:resultebutton.titleLabel.text]) 
     myScore += 1;  



    // NSLog(@"The button title is %@ ", correctAns); 
    // NSLog(@"The button title is %@ ", resultebutton.titleLabel.text); 

    NSString *finishingStatement = [[NSString alloc] initWithFormat:@"%i out of %i  correct!", myScore, totalQuestions]; 
    theScore.text = finishingStatement; 


    id nextQuestion = [appDelegate.qns objectAtIndex:qCount]; 

quizLbl.text = [nextQuestion labelQn]; 
    headerLbl.text = [nextQuestion labelHeader]; 

[qBtn setTitle:[nextQuestion labelBtn] forState:UIControlStateNormal]; 
[qBtnB setTitle:[nextQuestion labelBtnB] forState:UIControlStateNormal]; 
[qBtnC setTitle:[nextQuestion labelBtnC] forState:UIControlStateNormal]; 
[qBtnD setTitle:[nextQuestion labelBtnD] forState:UIControlStateNormal]; 



qCount++; 

} 


else { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Results" message:  [[NSString alloc] initWithFormat:@"You have answer %i questions correctly!", myScore] 
                delegate:self  cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    // alert.cancelButtonIndex = -1;      
    [alert show]; 

} 

} 

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

if (buttonIndex == 0){ 

QuizTableViewController *quizTable = [self.storyboard instantiateViewControllerWithIdentifier:@"quizTable"]; 


    UINavigationController *quizController = [[UINavigationController alloc] initWithRootViewController:quizTable]; 
    [quizController setModalPresentationStyle:UIModalPresentationFormSheet]; 
    [quizController setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; 

    [self presentViewController:quizController animated:YES completion:nil]; 
    } 

} 

답변

0

첫째 전역 변수의 값을 유지해서는 안 :

int totalQuestions, qCount, myScore; 

대신 그들에게 인스턴스 변수를 확인하십시오 (당신이 원하는 경우에이 방법을 사용하면 뷰 컨트롤러의 여러 사본을 가질 수 있습니다).

둘째는 totalQuestions를 재정의 한 것, 도움이되지 않습니다있는 (다른 형식으로!)을 분류 할 수

id totalQuestions = nil; 
if ([appDelegate.qns count] > 0){ 

...

관련 문제