"Applications are expected to have a root view controller.."


#1

Hi guys,

I ran in to this problem before and I am not sure what to do to fix it. Before I thought maybe it was just a typo in the BNR Objective-C book somewhere but now its showing up in the iOS book.

When I run my program, the first line of the console is always this:

2012-02-04 05:38:28.402 Quiz[581:f803] Applications are expected to have a root view controller at the end of application launch

I don’t really know what it means, but how do I fix it? I am using Lion and Xcode 4.2, and I downloaded and am using the Big Nerd Ranch template.


#2

What happens if you use their base quiz template?

bignerdranch.com/solutions/Quiz.zip

From this post:

http://forums.bignerdranch.com/viewtopic.php?f=76&t=1817


#3

The same thing happens when using the base quiz template.

It seems to be iOS 5 specific. If I run it in iOS 4.3 I do not get the error, but with iOS 5 I do.


#4

You don’t have to worry about it. In chapter 7, we introduce view controllers. You will understand it then. It doesn’t stop your app from running or do anything else. You can safely ignore it when working through the first six chapters.


#5

It doesn’t crash the app, but my app doesn’t display the location information in the console. Should I still continue through the chapter?


#6

I am just starting to learn iOS programming using the 2nd edition. My setup is Mountain Lion, Xcode 4.4.1 and iOS 5.1.1.

However, I am stuck at Chapter 1, the Quiz app. I created an empty application, added Application template from the File Template Library and created MainWindow.xib, and entered the code into QuizAppDelegate.[hm]:

#import <UIKit/UIKit.h>

@interface QuizAppDelegate : NSObject <UIApplicationDelegate>
{
   int currentQuestionIndex;
   
   NSMutableArray *questions;
   NSMutableArray *answers;
   
   IBOutlet UIWindow *window;
   IBOutlet UILabel  *questionField;
   IBOutlet UILabel  *answerField;
}

@property (retain, nonatomic) UIWindow *window;

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

@end
#import "QuizAppDelegate.h"

@implementation QuizAppDelegate

@synthesize window = _window;

- (id)init
{
   self = [super init];
   
   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 Denmark?"];
      [answers addObject:@"Copenhagen"];
      
      [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];
   [answerField setText:@"???"];
}

- (IBAction)showAnswer:(id)sender
{
   NSString *answer = [answers objectAtIndex:currentQuestionIndex];
   
   [answerField setText:answer];
}

- (void)dealloc
{
   [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
   // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
   // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
   // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
   // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
   // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
   // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

I linked the outlets and everything as instructed by the book. I have clean build but when the app runs, it gives me “2012-08-30 09:00:15.877 Quiz[7288:c07] Application windows are expected to have a root view controller at the end of application launch” and nothing appears on the simulator. I see only a blank white screen.

What is wrong? Have I missed something? I am totally confused and more confused after reading this forum as different solutions were suggested which are quite different from the book. For example, the book says enter this code:

- (id)init
{
   self = [super init];
   
   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 Denmark?"];
      [answers addObject:@"Copenhagen"];
      
      [questions addObject:@"From what is cognac made?"];
      [answers addObject:@"Grapes"];
   }
   return self;
}

… but the forum suggested otherwise:

#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;
}

as in the thread viewtopic.php?f=212&t=4283

Can you please help to enlighten me? Thanks.

Adrian Hoe


#7

I solved the problem by adding a row . Quiz-Info.plist:


i.stack.imgur.com/m3rR1.png

All buttons and labels are displaying but the buttons are not clickable. I checked my code in QuizAppDelegate.m and finds nothing unusual. Any pointers?

Thanks.

Adrian Hoe


#8

I have solved the problem by commenting out a chunk of codes in function didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];
    //[self.window makeKeyAndVisible];
    return YES;
}

Although my problems have been solved, I am still confused. Can someone please explain?

My only explanation is that Xcode 4.4.x and iOS5 differ from the tools which 2nd edition is based. Right?

Thanks.

Adrian Hoe


#9

@Adrian:
Commenting out these lines is not the best idea.

Xcode just wants you to set the root view controller. That can be done like this:

[[self window] setRootViewController:tvc];

But to set a root view controller you first need a view controller. Just go on reading and you’ll get to know about them.

Greetings
Joerg


#10

[quote=“JoergK”]@Adrian:
Commenting out these lines is not the best idea.

Xcode just wants you to set the root view controller. That can be done like this:

[[self window] setRootViewController:tvc];

But to set a root view controller you first need a view controller. Just go on reading and you’ll get to know about them.

Greetings
Joerg[/quote]

Hi Joerg,

What is tvc? I have no tvc declared and nothing about view controller has been mentioned in the first simple example in Chapter 1. Isn’t the book’s idea to introduce the simplest way to build a simplest app without view controller? I couldn’t even get the first simple example to work!

The first simple example in Chapter does not have any view controller at all. Why?

Why commenting out the chunk of statement isn’t a good idea?

If setting the root view controller is the best practice recommended by Apple, isn’t it a good idea to introduce view controller in the first example? At least a simple view controller as an introduction, more details about view controller can be introduced in later chapter.

Best regards,

Adrian Hoe


#11

Hi Adrian,
first of all: tvc was just an example from one of my projects. You can place any View Controller in that line.

That’s right. I just wanted to show you how you will set the root controller in the future. Upload the folder containing your project somewhere (dropbox for example) and I’m sure we’ll find a solution for your problem.

[quote] Why commenting out the chunk of statement isn’t a good idea?
[/quote]
These lines aren’t just for fun. I’ll give you an example:
You comment out “self.window.backgroundColor = [UIColor whiteColor];”. What happens? In the future (especially when working without Interface Builder - that’s often the case when you program games) you will often redraw the views on the screen. For example you have an image of a car. You redraw that car with 33 Frames per second - every Frame the car gets 5 pixels higher. Now the difference with and without that line of code.
With: The car “drives” as expected.
Without: The car will have a streak behind it. Every time it gets redrawn 5 pixels higher - but the old version doesn’t disappear because there is no background behind your views.

You don’t need a UIViewController for now. And having a view controller is also a little more work than you seem to expect. Just ignore that message by now - it doesn’t hurt and is just a recommendation. It’s not a good idea introducing everything at the same time; you didn’t start with vector calculation when you were a first grader, did you ? For now it’s important to understand just the most basic details. Ok ?

Greetings
Joerg


#12

[quote=“JoergK”]Hi Adrian,
first of all: tvc was just an example from one of my projects. You can place any View Controller in that line.

That’s right. I just wanted to show you how you will set the root controller in the future. Upload the folder containing your project somewhere (dropbox for example) and I’m sure we’ll find a solution for your problem.

[quote] Why commenting out the chunk of statement isn’t a good idea?
[/quote]
These lines aren’t just for fun. I’ll give you an example:
You comment out “self.window.backgroundColor = [UIColor whiteColor];”. What happens? In the future (especially when working without Interface Builder - that’s often the case when you program games) you will often redraw the views on the screen. For example you have an image of a car. You redraw that car with 33 Frames per second - every Frame the car gets 5 pixels higher. Now the difference with and without that line of code.
With: The car “drives” as expected.
Without: The car will have a streak behind it. Every time it gets redrawn 5 pixels higher - but the old version doesn’t disappear because there is no background behind your views.

You don’t need a UIViewController for now. And having a view controller is also a little more work than you seem to expect. Just ignore that message by now - it doesn’t hurt and is just a recommendation. It’s not a good idea introducing everything at the same time; you didn’t start with vector calculation when you were a first grader, did you ? For now it’s important to understand just the most basic details. Ok ?

Greetings
Joerg[/quote]

Hi Joerg,

It seems like commenting out the statements allow the Quiz example to work. How can I DB you my project?

Adrian Hoe


#13

I didn’t said that it won’t work when you comment out these lines. I just mentioned that it’s not a good idea. When you replace the big wheels of a truck with those of your normal car you can maybe drive it - but it’s not good.

It doesn’t need to be dropbox. Dropbox is just a service I use. If you want to use dropbox you need to register on their website (There is of course also an option to use it for free with limited space :wink: ) and then you can upload your project folder as a zip-File (rightclick in finder -> “Compress XYZ”). Then you can get a link to that file by rightclicking the zip-File on the website. If you register it should be self-explaining.

Greetings
Joerg


#14

[quote=“JoergK”]I didn’t said that it won’t work when you comment out these lines. I just mentioned that it’s not a good idea. When you replace the big wheels of a truck with those of your normal car you can maybe drive it - but it’s not good.

It doesn’t need to be dropbox. Dropbox is just a service I use. If you want to use dropbox you need to register on their website (There is of course also an option to use it for free with limited space :wink: ) and then you can upload your project folder as a zip-File (rightclick in finder -> “Compress XYZ”). Then you can get a link to that file by rightclicking the zip-File on the website. If you register it should be self-explaining.

Greetings
Joerg[/quote]

I’ve used DB for years.


#15

Than you know how to upload your project.

By the way:
I made you a program showing you the problem mentioned above: dl.dropbox.com/u/99449487/MovingDot.zip

Run it and then comment out the line I marked for you in AppDelegate.m - I know that it is not exactly the same line but basically it is.

Greetings
Joerg


#16

I’m also having issues with this and i can’t seem to find a suitable answer?

My application runs in the simulator - occasionally i get asked to confirm access for location settings but then the app does nothing?

The log in the debugger just shows the message: “2013-02-26 00:37:26.645 Whereami[6962:207] Applications are expected to have a root view controller at the end of application launch”

I can’t actually get the program further than this and generate a set of coordinates - what am I missing to get it working?
I don’t particularly want to push ahead when the next chapter continues to build on this program if I haven’t actually got it working yet…?

Thanks in advance,
Sn0wSt0rm

#import “AppDelegate.h”

@implementation AppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

    // Create my CLLocationManager object (allocate it some memory and initiate)
    locationManager = [[CLLocationManager alloc] init];

    // Set this file as the location to which CLLocationManager sends the location data
    [locationManager setDelegate:self];

    // Specify which results of the locationmanager we want to receive
    [locationManager setDistanceFilter:kCLDistanceFilterNone];

    // Specify level of accuracy - in this case s accurate as possible (regardless of how much time / battery is used to this end
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    // Send a message to the locationManager to start location tracking immediately
    [locationManager startUpdatingLocation];

    [[self window] makeKeyAndVisible];
    return YES;
    }

  • (void) locationManager:(CLLocationManager *)manager didUpdatetoLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{
NSLog(@"%@",newLocation);
}

  • (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{
NSLog(@“Could not find location %@”,error);

}

@end


#17

[quote=“JoergK”]Than you know how to upload your project.

By the way:
I made you a program showing you the problem mentioned above: dl.dropbox.com/u/99449487/MovingDot.zip

Run it and then comment out the line I marked for you in AppDelegate.m - I know that it is not exactly the same line but basically it is.

Greetings
Joerg[/quote]

I got the same issue here and I tried this program that you made it’s working good! thanks.
Squidoo for Marketers