Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 UINavigationController Unbalanced calls to begin/end appearance transitions for

I am using the following code to switch views in ViewDeck, it was completely fine in iOS5 and 6 but 7, when I try to pop to an exist view. The screen became all white/black.

-(void)switchViewWithViewController:(UIViewController*)viewControllerToSwitch
{
    if (viewControllerToSwitch)
    {
        // Reset Menu Button
        [self.viewDeckController closeLeftViewAnimated:YES completion:^(IIViewDeckController *controller)
         {
             [((BaseViewController*)viewControllerToSwitch) closeMenu];
         }];

        @try
        {
            [((UINavigationController*)self.viewDeckController.centerController) pushViewController:viewControllerToSwitch animated:NO];
        }
        @catch (NSException * ex)
        {
            //“Pushing the same view controller instance more than once is not supported”
            NSRange range = [ex.reason rangeOfString:@"Pushing the same view controller instance more than once is not supported"];

            if([ex.name isEqualToString:@"NSInvalidArgumentException"] && range.location != NSNotFound)
            {
                //view controller already exists in the stack - just pop back to it
                if (!IS_IOS7)
                {
                    [((UINavigationController*)self.viewDeckController.centerController) popToViewController:viewControllerToSwitch animated:NO];
                }
                else
                {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void)
                    {
                        [((UINavigationController*)self.viewDeckController.centerController) popToViewController:viewControllerToSwitch animated:NO];
                    });
                }
            }
        }
    }
}

I did try to add a delay 0.1 but it is not going to help. From the console, I found that it was popping two VCs at the same time.

Unbalanced calls to begin/end appearance transitions for <GameViewController: 0x15ef5630>.
-[BaseViewController viewDidAppear:] [Line 49] VC is showing: GameViewController
-[BaseViewController viewDidAppear:] [Line 49] VC is showing: HomePageViewController
like image 378
EES Avatar asked Nov 11 '22 21:11

EES


1 Answers

I admit that using try and catch is not a good practice. As the question is regarding to ViewDeck, so I just simply replace the centerViewController on the fly, and avoid the push pop stack error from UINavigationController.

Here is the code. Hope it can help someone.

-(void)switchViewWithViewController:(BaseViewController*)viewControllerToSwitch
{
    if (viewControllerToSwitch)
    {
        // Reset Menu Button
        [self.viewDeckController closeLeftViewAnimated:YES completion:^(IIViewDeckController *controller)
         {
             [viewControllerToSwitch closeMenu];
         }];

        UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:viewControllerToSwitch];
        self.viewDeckController.centerController = navVC;
    }
}
like image 103
EES Avatar answered Nov 15 '22 07:11

EES