siutsin
siutsin

Reputation: 1624

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

Upvotes: 5

Views: 4898

Answers (2)

siutsin
siutsin

Reputation: 1624

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;
    }
}

Upvotes: 1

uspython
uspython

Reputation: 563

hope you had fixed this problem.
1, I don't think using @try @catch to determine push or pop is an good idea.
2, I had this "Unbalanced calls to ...blabla" things too in iOS7. I my case ,I need force-rotate the screen then pop to last viewcontroller. In iOS 5 or 6, just force rotate in

-(void)viewWillDisappear
, it works fine but iOS7 crashed. So in iOS7 I made force rotate first then pop.
Depending on this https://stackoverflow.com/a/17440074/1343200,

"An animation is started before the last related animation isnt done."

Upvotes: 0

Related Questions