测验应用的随机问题

时间:2012-08-03 02:47:18

标签: xcode random xcode4.3

我想随机化我的测验问题,但它似乎不适用于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];
  }

 }

1 个答案:

答案 0 :(得分:0)

首先,你不应该在全局变量中持有值:

int totalQuestions, qCount, myScore;

改为使用实例变量(如果需要,可以使用视图控制器的多个副本)。

其次你似乎重新定义了totalQuestions(作为一种不同的类型!),这无济于事:

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

这可能会解决它......