5

我正在使用以下代码在 ViewDeck 中切换视图,当我尝试弹出到现有视图时,它在 iOS5 和 6 但 7 中完全没问题。屏幕变成全白/黑。

-(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];
                    });
                }
            }
        }
    }
}

我确实尝试添加延迟 0.1,但这无济于事。从控制台,我发现它同时弹出了两个 VC。

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
4

2 回答 2

1

我承认使用 try 和 catch 不是一个好习惯。由于问题是关于 ViewDeck 的,所以我只是简单地替换了 centerViewController,并避免了 UINavigationController 的推送弹出堆栈错误。

这是代码。希望它可以帮助某人。

-(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;
    }
}
于 2013-12-06T10:53:44.100 回答
0

希望你已经解决了这个问题。
1,我不认为使用@try @catch 来确定push 或pop 是一个好主意。
2,我在iOS7中也遇到了这种“对...blabla的不平衡调用”的东西。我的情况,我需要强制旋转屏幕然后弹出到最后一个视图控制器。在 iOS 5 或 6 中,只需强制旋转

-(void)viewWillDisappear
,它工作正常,但 iOS7 崩溃了。所以在iOS7中我先强制旋转然后弹出。
根据此https://stackoverflow.com/a/17440074/1343200

“动画在最后一个相关动画未完成之前开始。”

于 2013-11-01T03:34:44.487 回答