1

現在xcode5を使用しています。私は基本的にクイズアプリケーションを作成するつもりでした。質問の表示ボタンをクリックすると、配列に保存した次の質問が表示されます。しかし、それは私に(ヌル)を与え続けます。誰でもこの問題を解決するのを手伝ってもらえますか? 前もって感謝します。

//
//  Quiz2ViewController.h
//  Quiz2
//
//
#import <UIKit/UIKit.h>
@interface Quiz2ViewController : UIViewController
   {
    int currentQuestionIndex;

    NSMutableArray *questions;
    NSMutableArray *answers;

    IBOutlet UILabel *questionField;
    IBOutlet UILabel *answerField;
}
- (IBAction)showQuestion:(id)sender;
- (IBAction)showAnswer:(id)sender;
@end

.m ファイル

//
//  Quiz2ViewController.m
//  Quiz2
//
//
#import "Quiz2ViewController.h"

@interface Quiz2ViewController ()

@end

@implementation Quiz2ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

   //call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self){

    //Create two arrays and make the pointers point to them

    questions = [[NSMutableArray alloc] init];
    answers = [[NSMutableArray alloc] init];

   //Add questions and answers to the arrays
    [questions addObject:@"what is 7 + 7?"];
    [answers addObject:@"14"];

    [questions addObject:@"what is the capital of Vermont?"];
    [answers addObject:@"Montpelier"];

    [questions addObject:@"From what is cognac made?"];
    [answers addObject:@"Grapes"];}

    //return the address of the new object
    return self;
}

- (IBAction)showQuestion:(id)sender
{
    // Step to the next question
    currentQuestionIndex++;
    // Am i past the last Question?
    if (currentQuestionIndex == [questions count]){

    //Go back to the first question
    currentQuestionIndex = 0;
    }

    // Get the string at that index in the quetions array
    NSString *question = [questions objectAtIndex:currentQuestionIndex];

    // Log the strin gto the console
    NSLog(@"displaying question: %@",question);

    //display the stirng in the question field
    [questionField setText:question];

    //clear the answer field
    [answerField setText: @"???"];
}

-(IBAction)showAnswer:(id)sender
{
      //What is the answer to the current question?
    NSString *answer = [answers objectAtIndex:currentQuestionIndex];

    //Display it in the answer field
    [answerField setText:answer];
}

@end
4

1 に答える 1