2012-08-03 47 views
1

我想隨機化我的測驗問題,但它似乎沒有在viewDidLoad 工作我申請了斷點在ID 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){ 

這可能會整理出來...

相關問題