Display problem with Quiz


#1

Hi everyone,

I finished writing my code in Xcode, and I tried to run the Quiz with the simulator. The problem is that the label box for the question stays empty when I click on the associated button. However, when I click on the showAnswer button, the “???” disappear and then, when I click on the showQuestion button, the “???” go back, so I think the Outlets are set. Can you help me ? Here is my code in BNRQuizViewController.m

[code]#import “BNRQuizViewController.h”

@interface BNRQuizViewController ()

@property (nonatomic) int currentQuestionIndex;

@property (nonatomic, copy) NSArray *questions;
@property (nonatomic, copy) NSArray *answers;

@property (nonatomic, weak) IBOutlet UILabel *questionLabel;
@property (nonatomic, weak) IBOutlet UILabel *answerLabel;

@end

@implementation BNRQuizViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    bundle:(NSBundle *)nibBundleOrNil
    {
    // Call the init method implemented by superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
    // Create two arrays filled with questions and answers
    // and make the pointers point to them

      self.questions = @[@"From what is cognac made?",
                         @"What is 7+7?",
                         @"What is the capital of Vermont?"];
      
      self.answers = @[@"Grapes",
                       @"14",
                       @"Montpelier"];
    

    }

    // Return the adress of the new object
    return self;
    }

  • (IBAction)showQuestion:(id)sender
    {
    // Step to the next question
    self.currentQuestionIndex++;

    // Am I past the last question?
    if (self.currentQuestionIndex == [self.questions count]) {
    // Go back to the first question
    self.currentQuestionIndex = 0;
    }

    // Get the string at that index in the question array
    NSString *question = self.questions[self.currentQuestionIndex];

    // Display the string in the question label
    self.questionLabel.text = question;

    //Reset the answer label
    self.answerLabel.text = @"???";
    }

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

    // Display it in the answer label
    self.answerLabel.text = answer;
    }

@end
[/code]

Thanks for your support :slight_smile:


#2

I didn’t find my mistake :confused: Does anyone can help me?


#3

If you go to your xib file and right click on File’s Owner, do your outlets look similar to the one’s below? If not, you should clear them all and reconnect.


#4

first of all, I want to thank the OP for posting this question, since I encountered the same problem.

Eventually (I’m embarrassed to say how long it took me) I found that I had a typo in the initWithNibName method (I had called it ‘initWithNubName’)…

Strangely, i didn’t get any compiler errors or warnings for this.

Once I corrected the typo, it worked.


#5

That’s because the compiler had no way of knowing the INTENT was to override a superclass method. As far as it was concerned, you were creating a new method for your own use.

As you progress through the book, you’ll find more “opportunities” for this kind of behaviour. No errors, no warnings, but not the correct behaviour, due to a method not being called because of a typo in the name. Something to keep in mind…