Segue only working first time

Hi all!


I've got a modal segue that goes from the initial view controller to a main menu view controller programatically. Then if you log out of the app, it change the view controller to the inital, programatically too. Here comes the trouble, if the code goes again to the main menu de debugger shows me:


Warning: Attempt to present <MainMenu: 0x170bf000> on <InitViewController: 0x16e9c7f0> whose view is not in the window hierarchy!


And it stays in the InitViewController. I've searched in a lots of forums and i got no idea of what to do.


Code from the initViewController:



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ToMainMenu"]) {
        MainMenu *MM = (MainMenu *)segue.destinationViewController;
        MM.managedObjectContext = [self managedObjectContext];
    }
}


-(void)loadMainMenu{
    /
    [self performSelector:@selector(logedIn)
               withObject:nil
               afterDelay:0.0];
}
-(void)logedIn{
    [self performSegueWithIdentifier:@"ToMainMenu" sender:self];
}


Code from the Main menu:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UIButton *)sender
{
    if ([segue.identifier isEqualToString:@"bToSplashScreen"]){
        InitViewController *ivc = (InitViewController *)segue.destinationViewController;
        ivc.managedObjectContext = [self managedObjectContext];
    }
}


- (IBAction)touchCerrarSesion:(UIButton *)sender {
    [self performSegueWithIdentifier:@"bToSplashScreen" sender:self];
}


Here is how my storyboard is:




Please help! 😟

Accepted Answer

Well i got the solution by myself.


The point was that i have only one Navigation controller and all my segues are modal. So when i go to the initViewController, wich is the rootviewcontroller too, it seems to lost the hierarchy of the views. In the viewWillAppear method of this class i got this:



-(void)viewWillAppear:(BOOL)animated{
    [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}

This fixed the issue. Now it finds again all the views.

Segue only working first time
 
 
Q