启动错误时的iOS根控制器

时间:2012-03-10 04:34:29

标签: ios debugging viewcontroller

我在尝试测试运行应用时收到以下错误代码。

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

该应用程序作为一个裸shell应用程序运行正常(例如没有实现任何功能)但是一旦我向ViewController.m添加了函数,它就开始生成此错误。

ViewController.m包含。

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

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

我已经检查了一些其他问题并检查了main.m,但据我所知,一切看起来都很好。但就像我说它运行正常,直到我添加这些功能。

最后main.m是

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

1 个答案:

答案 0 :(得分:1)

我不知道你的BattTimeAppDelegate是使用NIB,Storyboard还是(自虐)代码来创建窗口,但是这个错误意味着你的UIWindow实例没有rootViewController。如果使用NIB或SB,则需要将Window的rootViewController链接到BattTimeViewController。如果使用代码(请不要),你需要做一些像“window.rootViewController = aHandMadeBattTimeViewController;”