如何停止弹出到导航控制器的标签栏的第二次点击?

发布于 2024-11-18 11:28:27 字数 1211 浏览 1 评论 0原文

我有一个基于标签栏的应用程序。所有选项卡都有一个导航控制器作为根。 如果用户再次点击该选项卡(如果该选项卡处于活动状态),它将弹回到导航控制器。

我怎样才能阻止这种行为?

所以实际上我有一个导航控制器 + 一个隐藏的 viewcontroller 来做出一些决定 + 另一个视图控制器。对于原始问题中的误导性信息,我们深表歉意。我对所有选项卡(其中 3 个)使用隐藏的 viewcontroller ,因为如果用户未登录,我在所有 3 个选项卡上都有登录屏幕。如果用户登录,那么我会弹出登录屏幕,并将 1,2,3 单独的 viewcontrollers 放在每个选项卡上。

第一次点击:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 did disappear  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  

第二次点击:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:Crossing  

I have a tab bar based app. All tabs have a navigation controller as the root.
If the user taps on the tab again if the tab is active, it pops back to the navigation controller.

How can I stop this behavior?

So in fact I have a navigation controller + a hidden viewcontroller that makes some decisions + another view controller. Sorry for the misleading information in the original question. I use the hidden viewcontroller for all the tabs, 3 of them, since I have the login screen on all 3 if the user is not logged in. If the user logs in, then I pop the login screen, and put the 1,2,3 individual viewcontrollers on each tab.

First tap:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 did disappear  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  

Second tap:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:Crossing  

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

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

发布评论

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

评论(5

一百个冬季 2024-11-25 11:28:27

@MarkGranoff 这样做是正确的,但方法是通过执行以下操作:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController.viewControllers indexOfObject:viewController] == tabBarController.selectedIndex)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

或者以更简洁的方式:

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

如果您只想阻止某个选项卡的默认行为,那么您可以执行类似的操作这:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSUInteger indexOfNewViewController = [tabBarController.viewControllers indexOfObject:viewController];
    // Only the second tab shouldn't pop home 
    return ((indexOfNewViewController != 1) ||
            (indexOfNewViewController != tabBarController.selectedIndex));
}

@MarkGranoff was on the right track for doing this, but the way to do it is by doing something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController.viewControllers indexOfObject:viewController] == tabBarController.selectedIndex)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

or in a less verbose way:

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

If you only want to block the default behaviour for a certain tab then you can do something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSUInteger indexOfNewViewController = [tabBarController.viewControllers indexOfObject:viewController];
    // Only the second tab shouldn't pop home 
    return ((indexOfNewViewController != 1) ||
            (indexOfNewViewController != tabBarController.selectedIndex));
}
木落 2024-11-25 11:28:27

Swift 3

  1. 子类化您的 UITabBarController 并在该类上实现 UITabBarControllerDelegate

    class viewMain: UITabBarController, UITabBarControllerDelegate {

  2. viewDidLoad 中,将类的委托设置为其自身

    self.delegate = self

  3. 添加此功能

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

Swift 3

  1. Subclass your UITabBarController and implement UITabBarControllerDelegate on that class.

    class viewMain: UITabBarController, UITabBarControllerDelegate {

  2. In viewDidLoad, set your class's delegate to be itself

    self.delegate = self

  3. Add this function

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

温馨耳语 2024-11-25 11:28:27

在您的应用程序委托(我假设是 UITabBarController 的委托)中,尝试实现 tabBarController:shouldSelectViewController: ,如果要选择的视图控制器不在其根视图,则返回 NO。当然,这可能会适得其反,因为当您尝试返回时,这些选项卡已被切换离开......嗯......

In your application delegate (which I assume is the delegate for your UITabBarController), try implementing tabBarController:shouldSelectViewController: and return NO if the view controller to be selected is not at its root view. This, of course, may backfire for tabs that were switched away from when you try to go back to them... Hmm....

绳情 2024-11-25 11:28:27

更新 Swift 4.1

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

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

停止双击所有选项卡。

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

谢谢!!!

Update Swift 4.1

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

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

Thanks!!!

千紇 2024-11-25 11:28:27

您可以检查视图控制器

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

并跟踪所选的视图控制器。

You can check for the view controller in

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

and you can keep track on the view controllers selected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文