Displaying question: (null)


#1

Hi, when I run the Simple Application (the Quiz application), the console is displaying

displaying question: (null)

In other words, (null) is appearing instead of the question from the array.

No errors are showing when I compile. I wonder if it has to do with the fact that my XCode is using a Main.storyboard file rather than the xib and nib files, and also the view controller uses a method that seems to expect a nib file, namely

Any help would be appreciated

[code]
#import <UIKit/UIKit.h>

@interface iosQuizViewController : UIViewController

iosQuizViewController.h
{
int currentQuestionIndex;

NSMutableArray *questions;
NSMutableArray *answers;

IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;

}

  • (IBAction)showAnswer:(id)sender;
  • (IBAction)showQuestion:(id)sender;

@end[/code]

iosQuizViewController.m

[code]#import “iosQuizViewController.h”

@interface iosQuizViewController ()

@end

@implementation iosQuizViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    questions = [[NSMutableArray alloc] init];
    answers = [[NSMutableArray alloc] init];
    [ 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 self;
    }

  • (IBAction)showQuestion:(id)sender
    {
    currentQuestionIndex++;
    if (currentQuestionIndex == [ questions count] ){
    currentQuestionIndex = 0;
    }
    NSString *question = [ questions objectAtIndex:currentQuestionIndex];
    NSLog(@“displaying question: %@”, question);
    [questionField setText: question];
    [answerField setText:@"???"];

}

  • (IBAction)showAnswer:(id)sender
    {
    NSString *answer = [ answers objectAtIndex:currentQuestionIndex];

    [answerField setText:answer];
    }

@end[/code]


#2

Yes, you are right.

I am currently using Xcode 5.0

Instead of using

Use [super viewDidLoad];

And leave off the return self at the bottom and then it will work as expected. Everything else is the same.

So the top part of QuizViewController.m should be:

- (void)viewDidLoad

{
    [super viewDidLoad];
    
    if (self) {
       //Create two array 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"];           
    }    
}