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

 }
4

1 回答 1

0

首先,您不应该在全局变量中保存值:

int totalQuestions, qCount, myScore;

改为使它们成为实例变量(这样,如果需要,您可以拥有视图控制器的多个副本)。

其次,您似乎已经重新定义totalQuestions(作为不同的类型!),这无济于事:

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

这样可以解决...

于 2012-08-03T12:39:25.973 回答