UITabBarController的默认行为是在第二次点击特定选项卡时将包含的UINavigationController弹出到根视图控制器。我有一个特殊的用例,我希望这不会自动运行,而且我很难弄清楚如何防止这种情况。
有没有人碰到这个,如果有的话,你做了什么?我是否需要继承UINavigationController并覆盖popToRootViewController或者是否有更简单的方法?
答案 0 :(得分:63)
使用tabBarController:shouldSelectViewController:的UITabBarControllerDelegate protocol方法。
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
return viewController != tabBarController.selectedViewController;
}
不要忘记将标签栏控制器的委托设置为实际实现此委托方法的对象。
答案 1 :(得分:14)
这就是我所做的:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)
return NO;
return YES;
}
问候
答案 2 :(得分:2)
更新Swift 4.1
停止双击所有标签。
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
//for blocking double tap on all tabs.
return viewController != tabBarController.selectedViewController
}}
停止双击只有一个特定的选项卡。这是第3个标签页。
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
//for blocking double tap on 3rd tab only
let indexOfNewVC = tabBarController.viewControllers?.index(of: viewController)
return ((indexOfNewVC != 2) ||
(indexOfNewVC != tabBarController.selectedIndex))
}}
希望有帮助...
谢谢!
答案 3 :(得分:1)
这种行为有点奇怪,但是在深层次结构的情况下是一个方便的快捷方式!
您可以实现以下UITabBarControllerDelegate方法来禁用此系统范围的快捷方式:
#pragma mark -
#pragma mark UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
UIViewController *tbSelectedController = tbc.selectedViewController;
if ([tbSelectedController isEqual:vc]) {
return NO;
}
return YES;
}
答案 4 :(得分:0)
这是Swift 3版本:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
return viewController != tabBarController.selectedViewController
}