Home:ALL Converter>View Controller reloading instead of dismissing

View Controller reloading instead of dismissing

Ask Time:2015-01-06T12:26:58         Author:nserror

Json Formatter

I have a view controller, that modally presents a navigation controller. The navigation controller has 2 view controllers.

Each of these 3 view controllers are delegated in a chain.

The top most view controller, VC3, I want to dismiss all the way back to the view controller that presented the navigation controller, VC1.

So in VC3 I call to my delegate which is VC2

[self.delegate newGameViewController:self submitRound:nil forGame:self.game];

VC2 reacts

-(void)newGameViewController:(NewGameViewController *)newGameViewController submitRound:(NSDictionary *)round forGame:(Game *)game
{
    NSLog(@"Delegate Called");
    [self.navigationController popViewControllerAnimated:NO];
    [self.delegate turnViewController:self didCompleteTurnWithStats:[self statsWithOutcome:_wonTurn]];

}

This should pop the top view of the navigation controller, which is VC3. Then call its delegate which is VC1 - which presented the UINavigationController

In VC1

-(void)turnViewController:(TurnViewController *)turnViewController didCompleteTurnWithStats:(NSDictionary *)stats
{
     [self.turnNav dismissViewControllerAnimated:NO completion:^{
         [self updateData];
     }];
}

This should dismiss the entire navigation controller, and VC1 should be the visible view controller.

But THIS IS NOT THE CASE.

Instead, it reloads a new instance of VC3.

This only happens when the phone locks whenever VC3 is visible and then is reopened, or the app is switched to the background and back. I followed Apple's guidelines of only dismissing view controllers by the controllers that presented them - but it still appears that my presenting controllers are being deallocated? At least thats what I am assuming is happening.

Is there anyway to prevent this? I tried registering for notifications for name:UIApplicationDidEnterBackgroundNotification to try and dismiss before this happens - but it only triggers half the time.

Thanks

EDIT: Here is the code of my updateData method in VC1 - that is called before dismissing the navigation controller. I updated the code to call it after the navigation controller had been dismissed. No change to the problem.

-(void)updateData
{
    [[GameManager instance] getGame:self.game withCompletion:^(Game *game, BOOL completed){
    if(completed)
    {
        if(game.played){
            [self loadGameComplete];
        } else {
            if(!roundsLoaded)
                [self shouldLoadRounds];

            if(UILoaded && roundsLoaded)
                [self updateUI];

            if(self.game.lastTurn.played && self.game.myTurn)
                [self shouldSendPhoto];
        }
    } else {
        if(!roundsLoaded)
            [self loadError];
    }
}];
}

EDIT 2: I am now explicitly dismissing the navigation controller, which I have a strong reference too, in VC1. Still reloading VC3 instead of dismissing.

Here is my method in VC1 that loads the navigation controller with VC2 as the root.

-(void)loadTurn:(Turn *)turn
{
    if(turn.played){ [self shouldSendPhoto]; return; }

    TurnViewController *turnVC = [[TurnViewController alloc] initWithGame:self.game andTurn:turn];
     turnVC.delegate = self;
     self.turnNav = [[UINavigationController alloc] initWithRootViewController:turnVC];
     self.turnNav.navigationBar.translucent = NO;
     if(IS_IPHONE_4_OR_LESS)
         self.turnNav.navigationBarHidden = YES;

     [self presentViewController:self.turnNav animated:YES completion:nil];
 }

Author:nserror,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27792079/view-controller-reloading-instead-of-dismissing
yy