Search code examples
uiviewcontrollerios6xcode4.5xibuistoryboardsegue

why is my viewcontroller not in the window hierarchy?


Using storyboards, MainMenuViewController presents Number1ViewController modally with this:

-(void) goToNumbers {

    [self performSegueWithIdentifier: @"seguetonumbers" sender: self];

}

Then Number1ViewController presents Number2ViewController like this:

-(void)nextLevel {

     [self performSegueWithIdentifier: @"segueToLevel2" sender: self];
}

xCode then produces this warning:

2013-01-23 12:09:49.873 ToddlerTeacherMini[7574:907] Warning: Attempt to present <Number2ViewController: 0x1fddc720> on <Number1ViewController: 0x1fdc7380> whose view is not in the window hierarchy!

Evertything I see online about this warning says that you cannot present a VC in another VC's viewDidLoad method and moving your code to the viewWillAppear method will fix this. Im not calling this segue from viewDidLoad but rather im calling it way later so Im not sure why this is happening.

I present VC's similarly elsewhere in my app without issue and cant figure out what is different here, any help?

To be clear everything appears to work as expected in my app I just dont want to ignore this message and have it come back and bite me later.


Per Todd Kerpelman's suggestion I looked into where my nextLevel method was getting called. Breakpoint didnt tell me much but digging a little further with NSLog I came up with this:

-(void)nextLevel {


    if (nextLevelHasNotBeenCalled == 0){

       [self performSegueWithIdentifier: @"segueToLevel2" sender: self];
        NSLog(@"Segue was called here.");

    }

    nextLevelHasNotBeenCalled ++;
    NSLog(@"Next level has been called %i times!", nextLevelHasNotBeenCalled);


}

Log:

2013-01-26 02:04:12.579 ToddlerTeacherMini[9203:907] Segue was called here.
2013-01-26 02:04:12.593 ToddlerTeacherMini[9203:907] Next level has been called 1 times!
2013-01-26 02:04:14.789 ToddlerTeacherMini[9203:907] Next level has been called 2 times!

Pretty clear now that nextLevel is getting called twice and is what is causing my problem.


Solution

  • This sounds like a case of your code accidentally calling nextLevel when you're not expecting it to. (Probably as a side effect of some unrelated method being called in your Number1ViewController's viewDidLoad method.)

    Did you try adding a breakpoint to your nextLevel method and seeing how/where it's getting called? That's probably the best way to at least confirm (or eliminate) this possibility.