ios9应用程序窗口应具有根视图控制器

时间:2015-11-13 09:57:06

标签: ios objective-c ios9

发布后我遇到了崩溃:

  

2015-11-13 17:47:50.744 app [18380:611105] ***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:   '应用程序窗口应具有根视图控制器   应用程序启动结束'

我已经阅读了ios9和root视图控制器的其他问题,现在我的appdelegate:

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    MainViewController *main = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];

    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *window in windows) {
        NSLog(@"window: %@",window.description);
        if(window.rootViewController == nil){
            UIViewController *vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
            window.rootViewController = vc;
        }
    }

    self.window.rootViewController = main;
    [self.window setRootViewController:main];
    [self.window makeKeyAndVisible];

    return YES;
}

不幸的是问题仍然存在。

1 个答案:

答案 0 :(得分:0)

也许尝试摆脱循环:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.backgroundColor = [UIColor whiteColor];

    MainViewController *main = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    window.rootViewController = main;
    self.window = window;

    [window makeKeyAndVisible];
    return YES;
}