QuizViewController not initializing?


#1

having trouble tracking down an issue for the first example… Everything compiles fine and appears to run fine, but does not appear to be using the designated initializer for QuizViewController, as the question and answer arrays are not initialized. Inserted an NSLog at the beginning of the initWithNibName:bundle method in QuizViewController.m and it is not logged in the console. Tried the example over again in xcode 3.2.6… same thing… and wow did they make some big improvements in xcode 4! I’ve only read ahead in the book a bit, but I must be missing something… assuming UIViewController’s designated initializer is initWithNibName:bundle, how can it not be using the one I’ve created?

P.S. First attempt was w/xcode 4.3 and iOS 5 Sim, so don’t think it is that


#2

Hm. Are you seeing the interface? Are you sure you have spelled the initializer correctly? Try pasting your QuizViewController.m in here.


#3

[code]#import “QuizViewController.h”

@implementation QuizViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    NSLog(@“initializing instance of quizViewController”);
    // Call the init method implemented by the superclass
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // create two arrays and make 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 questions array
    NSString *question = [questions objectAtIndex:currentQuestionIndex];

    // Log the string to the console
    NSLog(@“displaying question: %@”, question);

    // Display the string in the question field
    [questionField setText];

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

    NSLog(@“displayed answer: %@”, answer);

}

@end
[/code]

and here is QuizViewController.h:

#import <UIKit/UIKit.h>

@interface QuizViewController : UIViewController {

	int currentQuestionIndex;
	
	// The model objects
	NSMutableArray *questions;
	NSMutableArray *answers;
	
	// The view objects
	IBOutlet UILabel *questionField;
	IBOutlet UILabel *answerField;
}

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

@end


#4

Would a moderator please post a tarball of the quiz project source so I can eliminate user error and ( hopefully) get to the bottom of this?


#5

The solutions file accidentally got the wrong project for this particular chapter, I’ll fix that tomorrow.

Does the QuizAppDelegate.m file actually create the instance of QuizViewController?


#6

Think so… Looks good in the simulator, buttons work, methods work, arrays just null and labels blank (except for ??? for the answer label as set in the action).


#7

yes, definitely creating a QuizViewController instance… I think it is just using the UIViewController designated initializer and thus not populating the Q&As.


#8

Well, I copied and pasted your initializer into a project and it worked fine. And once you have overridden the designated initializer of UIViewController, it’s always going to get called.

So you never get the console output “initializing instance of quizViewController”"?

A long shot, but try cleaning your project (Clean from the Product menu) and running it again. Otherwise, without having access to the project and being able to build it on my own, I don’t know if I can figure it out from here. We should have the solution for this chapter online soon.


#9

[quote=“JoeConway”]Well, I copied and pasted your initializer into a project and it worked fine. And once you have overridden the designated initializer of UIViewController, it’s always going to get called.

So you never get the console output “initializing instance of quizViewController”"?

A long shot, but try cleaning your project (Clean from the Product menu) and running it again. Otherwise, without having access to the project and being able to build it on my own, I don’t know if I can figure it out from here. We should have the solution for this chapter online soon.[/quote]

I do not get the console output in the initializer, but I do get one if I insert it in QuizAppDelegate.m in the didFinishLaunchingWithOptions:. The app does initialize a QuizViewController (just not with my designated initializer?)… very strange. The buttons show up, are responsive and bound to the appropriate methods. I’ll try cleaning the project tonight and try the solution when that is up… very frustrating :confused:

I may have to skip ahead to the ‘using the debugger’ chapter to see what is going on.


#10

cleaning the project had no effect… off to read the debugger chapter!


#11

definitely not using the designated QuizViewController initializer… but the problem, as expected, was not in the code typed from the book. I’m fairly convinced that I created the project prior to upgrading to the newest version of Xcode and the ios5 simulator. The auto-created code is significantly different for my Quiz and the BNR solution files (which works fine)… when I create a new project, the files created look like the solution files as opposed to my Quiz app that isn’t initializing… So, at this point, I can either dig into trying to understand which difference in the delegate, main.m and xib files created the problem, or continue with the book.
.
.
.
off to Chapter 5. :smiley:

P.S. thanks for the help Mr. Conway… I’m really enjoying your writing style as well.


#12

E-mail me a zip of your code, I’ll be able to hunt it down. Just my first name at bignerdranch.com.


#13

I’ve hit this same problem. I’m using XCode 3. It seems like this is where the problem is, as different initialization code is being generated in QuizAppDelegate.m.

initWithNibName was not executing as nothing from QuizAppDelegate was calling it. Adding the following line to the application method fixed the problem:

[code]    [viewController initWithNibName:@"q2ViewController.nib" bundle:nil];[/code]

Alternatively, without making this change and overriding viewDidLoad instead of initWithNibName in QuizViewController achieved the same thing.

Pain to have to figure this out for the first app, when I expected it to just work!


#14

Sorry, I’d like to correct my previous post (confused by trying too many alternatives):

Overriding awakeFromNib instead of viewDidLoad was the cleaner solution an worked fine.