0

After launch I have a crash:

2015-11-13 17:47:50.744 app[18380:611105] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'

I've read other questions with ios9 and root view controller and now my 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;
}

Unfortunately the problem is still there.

Otávio
  • 735
  • 11
  • 23
suzrik
  • 1
  • 3
  • 2
    Do you have `self.window.makeKeyAndVisible()`? – tuledev Nov 13 '15 at 10:00
  • Yes, of course. [self.window makeKeyAndVisible]; – suzrik Nov 13 '15 at 10:02
  • `self.window.rootViewController = main; [self.window setRootViewController:main];` are the same. You are using `for(UIWindow *window in windows) {` for what? – tuledev Nov 13 '15 at 10:04
  • I know the same thing. I just showed that tried all the options. – suzrik Nov 13 '15 at 10:05
  • Can you please post the function fully? – tuledev Nov 13 '15 at 10:06
  • for(UIWindow *window in windows) { it is one of the solutions that has been here in other question. – suzrik Nov 13 '15 at 10:06
  • I've updated the post. Now there is the whole function. – suzrik Nov 13 '15 at 10:10
  • Did you check this solution? http://stackoverflow.com/questions/7520971/applications-are-expected-to-have-a-root-view-controller-at-the-end-of-applicati – tuledev Nov 13 '15 at 10:12
  • I've moved all code from viewdidload to viewdidappear and now all works fine. Big thanks! I was trying to show alert on display of a view before it actually came on screen. It works in earlier version, but crush in ios9. Thanks again! – suzrik Nov 13 '15 at 10:22

1 Answers1

0

Maybe try getting rid of the loops:

- (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;
}
dpigera
  • 3,339
  • 5
  • 39
  • 60