0

For some reason, whenever I try to run my app, the app will always go to LoginViewController even though it's supposed to go to ViewController when passwordCheck has a non-null value.

Warning: Attempt to present <UINavigationController: 0x14570ff0> on <LoginViewController: 0x1465ea00> whose view is not in the window hierarchy!

This was working before until I switched to using Storyboards from .xib files. Here's the code that I have (it is currently in LoginViewController.m.

if (passwordCheck) {
        NSLog(@"%@", usernameField.text);

        // Persist the Username for recovery later
        [[NSUserDefaults standardUserDefaults] setObject:usernameField.text forKey:kUsernameDefaultsKey];
        [[NSUserDefaults standardUserDefaults] synchronize];


        NSError *error = nil;
        [STKeychain storeUsername:[NSString stringWithFormat:@"%@", usernameField.text] andPassword:[NSString stringWithFormat:@"%@", passwordField.text] forServiceName:@"LoginApp" updateExisting:YES error:&error];

        ViewController *viewControl = [self.storyboard instantiateViewControllerWithIdentifier:@"MainControllerNav"];

        // Send username to ViewController

        AppDelegate *dataCenter = (AppDelegate *) [[UIApplication sharedApplication] delegate];

        dataCenter.usernameData = usernameField.text;

        NSLog(@"%@", dataCenter.usernameData);

        viewControl.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:viewControl animated:YES completion:nil];

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    } else {

        NSLog(@"Finish viewDidLoad and wait for future instructions from user.");
}

I tried to fix it using this answer, but neither have been helpful. How do I solve this? Thank you in advance.

Community
  • 1
  • 1
chrisjr
  • 760
  • 3
  • 11
  • 18
  • your navigationcontroller should be rootviewcontroller of window otherwise run time you will get this error. – Tirth Nov 22 '13 at 16:20
  • Why are you using `self presentViewController` in that code? And the question doesn't match the title - your login view is already shown... – Wain Nov 22 '13 at 16:29

1 Answers1

0

Where do you have this code? In viewDidLoad? If so, that's your problem. You can't present another controller before the current one has been added to the window's hierarchy. Put the code in viewDidAppear, and present the next controller with no animation if you don't want to see the login controller.

rdelmar
  • 103,982
  • 12
  • 207
  • 218