防止双击 UITabBarController 时自动 popToRootViewController

发布于 2024-08-13 03:47:54 字数 220 浏览 2 评论 0原文

UITabBarController 的默认行为是当第二次点击特定选项卡时将包含的 UINavigationController 弹出到根视图控制器。我有一个特定的用例,我希望它不会自动工作,并且我很难弄清楚如何防止这种情况。

有人遇到过这种情况吗?如果有,你做了什么?我是否需要子类化 UINavigationController 并覆盖 popToRootViewController 还是有更简单的方法?

The default behavior of a UITabBarController is to pop the contained UINavigationController to the root view controller when a particular tab is tapped a second time. I have a particular use case where I'm wanting this to not work automatically, and I'm having a hard time figuring out how to prevent this.

Has anyone run into this, and if so, what did you do? Do I need to subclass UINavigationController and override popToRootViewController or is there a simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满栀 2024-08-20 03:47:54

使用 tabBarController:shouldSelectViewController: UITabBarControllerDelegate 协议

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}

不要忘记将选项卡栏控制器的委托设置为实际实现此委托方法的对象。

Use the tabBarController:shouldSelectViewController: method of the UITabBarControllerDelegate protocol.

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}

Don't forget to set the delegate of the tab bar controller to the object that actually implements this delegate method.

天邊彩虹 2024-08-20 03:47:54

这就是我所做的:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{

    if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)

            return NO;

    return YES;

}

问候

this is what I did:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController 
{

    if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)

            return NO;

    return YES;

}

regards

倾听心声的旋律 2024-08-20 03:47:54

更新 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
}}

停止仅双击一个特定选项卡。这是第三个选项卡。

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

希望有帮助...

谢谢!

Update Swift 4.1

Stop Double Tap for all tabs.

extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    //for blocking double tap on all tabs.
    return viewController != tabBarController.selectedViewController
}}

Stop Double Tap on only one specific tab. Here it's for 3rd Tab.

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

Hope it helps...

Thanks!!!

呢古 2024-08-20 03:47:54

这种行为有点奇怪,但在层次结构很深的情况下,这是一个方便的捷径!

您可以实现以下 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;
}

This behavior is a little strange, but a handy shortcut in case of deep hierarchy!

You can implement following UITabBarControllerDelegate methods to disable this system wide shortcut:

#pragma mark -
#pragma mark UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
    UIViewController *tbSelectedController = tbc.selectedViewController;

    if ([tbSelectedController isEqual:vc]) {
        return NO;
    }

    return YES;
}

这是 Swift 3 版本:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return viewController != tabBarController.selectedViewController
}

Here is the Swift 3 version:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return viewController != tabBarController.selectedViewController
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文