I’m recieving the following error code when trying to test run an app.
Application windows are expected to have a root view controller at the end of application launch
The app was running fine as a bare shell app (e.g. no functionality implemented) but once I added functions to ViewController.m it started generating this error.
ViewController.m contains.
//
// BattTimeViewController.m
// BattTime
//
// Created by James Krawczyk on 09/03/2012.
// Copyright (c) 2012 James Krawczyk. All rights reserved.
//
#import "BattTimeViewController.h"
@interface BattTimeViewController ()
@end
@implementation BattTimeViewController
@synthesize resDisplay;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setResDisplay:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)batteryStatus
{
// Get current date time
datetrack = [NSDate date];
// Instantiate a NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Set the dateFormatter format
//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// or this format to show day of the week Sat,11-12-2011 23:27:09
[dateFormatter setDateFormat:@"HH:mm:ss"];
// Get the date time in NSString
NSString *dateInStringFormated = [dateFormatter stringFromDate:datetrack];
NSLog(@"%@", dateInStringFormated);
// Release the dateFormatter
//[dateFormatter release];
NSArray *batteryStatus = [NSArray arrayWithObjects:
@"Battery status is unknown.",
@"Battery is in use.",
@"Battery is charging.",
@"Battery is fully charged.", nil];
if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown)
{
[resDisplay setText:[batteryStatus objectAtIndex:0]];
NSLog(@"%@", [batteryStatus objectAtIndex:0]);
}
else
{
NSString *msg = [NSString stringWithFormat:
@"Battery charge level: %0.2f%%\n%@", [[UIDevice currentDevice] batteryLevel] * 100, dateInStringFormated ];
[resDisplay setText:msg];
NSLog(@"%@", msg);
}
}
- (void)loadView
{
// Enable monitoring of battery status
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
// Print current status
[self batteryStatus];
// Request to be notified when battery charge or state changes
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
ViewController.h is;
//
// BattTimeViewController.h
// BattTime
//
// Created by James Krawczyk on 09/03/2012.
// Copyright (c) 2012 James Krawczyk. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BattTimeViewController : UIViewController
{
NSDate *datetrack;
}
@property (weak, nonatomic) IBOutlet UILabel *resDisplay;
@end
I’ve checked some other questions and checked the main.m too but as far as I know everything looks fine on there. However like I said it was running fine until I added those functions.
Lastly main.m is
//
// main.m
// BattTime
//
// Created by James Krawczyk on 09/03/2012.
// Copyright (c) 2012 James Krawczyk. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "BattTimeAppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BattTimeAppDelegate class]));
}
}
I don’t know if your BattTimeAppDelegate is using a NIB, a Storyboard or (masochistic) code to create the window, but this error means your UIWindow instance does not have a rootViewController. If using NIB or SB, you need to link the Window’s rootViewController to your BattTimeViewController. If using code (please don’t), you need to do something like “window.rootViewController = aHandMadeBattTimeViewController;”