Warning: Attempt to present whose view is not in the window hierarchy! When using identified segue

AndyOS picture AndyOS · Nov 28, 2013 · Viewed 10.7k times · Source

I know there are lots of other questions concerning this but they don't fully explain my issue.

I go from my initial view controller SAGViewController to another SAGHomeScreenViewController via an identified storyboard custom segue, when the relevant button is pressed :

-(IBAction)goNewGame{
    [self performSegueWithIdentifier:@"startHomeSegue" sender:self];
}

It works fine but I still get the warning:

Warning: Attempt to present SAGHomeScreenViewController: 0xc43b800 on SAGViewController: 0xbaaa9f0 whose view is not in the window hierarchy!

If I comment out the performSegueWithIndentifer, the segue still works but I don't get the warning:

-(IBAction)goNewGame{
    //[self performSegueWithIdentifier:@"startHomeSegue" sender:self];
}

Why? Note that I don't have a navigation controller.
Any advice here would be appreciated, the segue still works but I don't want any warnings that I don't understand!

Answer

Greg picture Greg · Nov 28, 2013

You should remove that method and you should make connection in your storyboard from button to the second viewController.

//EXTENDED

If you want to pass something to your destination view you use prepareForSegue:sender method. This method is called every time you change the view via segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"YOURSEGUEIDENTIFIER"])
    {
        YourViewController *yourVC = [segue destinationViewController];
    }
}

//EXTENDED

This is happening because you have connected your segue in two ways one is in code and another is in storyboard. You have to remove one of those. You can remove your IBAction from your code (so you will have just connection in storyboard) or you can remove your connection in storyboard (from your button to destination view controller) and add it again but this time drag it from your source view controller (not button) to your destination view controller. Don't forget set up segue identifier. If you use storyboard I would recommend you to remove your IBAction from code, less code to manage. Hope this help.