show问题显示问题(null)而不是存储在数组中的问题

时间:2013-09-25 03:40:50

标签: ios objective-c storyboard

我目前正在使用xcode5。我基本上打算创建一个测验应用程序。当我点击显示问题按钮时,它应该给我下一个存储在数组中的问题。但是,它一直给我(null)。任何人都可以帮我解决这个问题吗?提前谢谢。

//
//  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

1 个答案:

答案 0 :(得分:3)

我知道这是两个月的时间,但对于那些通过Big Nerd Ranch iOS编程书工作的人来说,这是一个常见的问题。永远不会调用initNibWithName方法,因为您没有使用NIB文件 - 您正在使用故事板。要使此代码工作,只需删除方法

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

然后将代码放在viewDidLoad块中,如此

- (void)viewDidLoad
{
    [super viewDidLoad];
    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"];}

    }
}