0

I'm using parse and I'm trying to get the login screen to show if the user isn't the "current" user. I'm having issues with my NavigationController (from my storyboard) and using it as the rootViewController even though it's already set as initial View Controller in my storyboard. Using this line of code I select the NavigationController (from my storyboard) and initialize it in my app delegate.

 UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;

I then decide whether or not to display the loginVC then finally I set the NavigationController as the rootViewController here:

self.window.rootViewController = navVC; [self.window makeKeyAndVisible];

Except someone I don't. I get this error when I try to build my app. "Application windows are expected to have a root view controller at the end of application launch" Anyone have any ideas what's going wrong?

Here's the code all together.

AppDelegate.m

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;

    // Initialize Parse.
    [User registerSubclass];
    [Question registerSubclass];
    [Parse setApplicationId:@"HI"
                  clientKey:@"HI"];

    // Determine whether or not to show the login screen
    if (![PFUser currentUser]) {
        LogInViewController *loginVC = [[LogInViewController alloc] init];
        [navVC setViewControllers:@[loginVC] animated:YES];
    } else {
        QuestionsTableViewController *questionsVC = [[QuestionsTableViewController alloc] init];
        [navVC setViewControllers:@[questionsVC] animated:YES];
    }

    self.window.rootViewController = navVC; [self.window makeKeyAndVisible];

    return YES;
}
Wesley Cho
  • 495
  • 8
  • 23

2 Answers2

0

If you're starting with a storyboard you need to have it set as your Main Interface in the Deployment Info in your app's General settings. You should also remove the line:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

..as it's initialising a new window in the place of the one created from the previous step.

GraemeArthur
  • 507
  • 5
  • 13
0

I used these two lines of code to fix the issue. I got answer from here: Programmatically set the initial view controller using Storyboards

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    UINavigationController *navVC = [storyboard instantiateViewControllerWithIdentifier:@"QuestionsView"];

I removed this line:

UINavigationController *navVC = (UINavigationController *)self.window.rootViewController;

There don't get the error anymore.

Community
  • 1
  • 1
Wesley Cho
  • 495
  • 8
  • 23