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;

代わりにそれらをインスタンス変数にします (このようにして、必要に応じて View Controller の複数のコピーを作成できます)。

第二に、(別のタイプとして)再定義したようですがtotalQuestions、これは役に立ちません:

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

それで解決するかも…

于 2012-08-03T12:39:25.973 に答える