Builds, but it cant find "root view controller"


#1

Code compiled, and iOS Simulator came up, but was empty. The console had the following:

“Application windows are expected to have a root view controller at the end of application launch”

I tried putting some NSLog’s in initWithNibName:bundle to see if program ever got there, but it never did.

thanks for any help!

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

@interface QuizViewController : UIViewController
{
int currentQuestionIndex;

//The model objects
NSMutableArray *questions;
NSMutableArray *answers;

//the view objects - don't worry about IBOutlet
//we'll talk about it shortly
IBOutlet UILabel *questionField;
IBOutlet UILabel *answerField;

}

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

@end
[/code]

[code]#import “QuizViewController.h”

@implementation QuizViewController

//when app opens, it gets sent this msg: initWithNibName…

  • (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    //call the init method implemented by the superclass
    NSLog(@“initializing”);
    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"];
      
      NSLog(@"answers & Questions created");
    

    }

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

}

@end
[/code]


#2

I figured it out. I think some others got it. The app delegate wasn’t calling initWithNibName

Had to add several lines to the AppDelegate

Figured it out by looking at BNR’s solutions from
bignerdranch.com/solutions/i … ing3ed.zip

sorry if this was already covered

[code]#import <UIKit/UIKit.h>
@class QuizViewController;

@interface QuizAppDelegate : UIResponder

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) QuizViewController *viewController;

@end
[/code]

and appDelegate.m looks like

#import "QuizAppDelegate.h"
#import "QuizViewController.h"

@implementation QuizAppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[QuizViewController alloc] initWithNibName:@"QuizViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    NSLog(@"Application didFinishLaunchingWithOptions executed");
    return YES;
}