我正在使用以下代码在 ViewDeck 中切换 View ,在 iOS5 和 6 中完全没问题,但在 iOS 7 中,当我尝试弹出到现有 View 时。屏幕变成全白/黑。
-(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
最佳答案
我承认使用 try 和 catch 不是一个好的做法。由于问题是关于 ViewDeck 的,所以我只是简单地即时替换 centerViewController,并避免 UINavigationController 的 push pop 堆栈错误。
这是代码。希望它可以帮助某人。
-(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;
}
}
关于iOS 7 UINavigationController 不平衡调用开始/结束外观转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18891412/