【问题标题】:iphone app - detect which tab bar item was pressediphone应用程序-检测按下了哪个标签栏项目
【发布时间】:2011-08-18 15:28:30
【问题描述】:

我有一个基于标签栏的应用程序,其中包含超过 5 个标签栏项目 - 所以我可以直接在视图中显示其中的 4 个,其余的可以通过选择“更多”标签获得。当一个标签栏项目被按下时,我想检测它是哪一个。
所以,在
- (void)tabBarController:(UITabBarController *)tabBarCtrl didSelectViewController:(UIViewController *)viewController 方法中,我使用tabBarCtrl.selectedViewController.title 来获取项目的标题。

这适用于视图中可见的选项卡 - 即第 4 个和“更多”选项卡 - 但不适用于按“更多”选项卡后显示在列表中的其余选项卡栏项目.

我可以看到,从“更多”列表中选择选项卡时,甚至没有调用 didSelectViewController 方法。
按下时如何检测到它们中的任何一个?

提前谢谢你。

【问题讨论】:

    标签: objective-c tabbar uitabbaritem


    【解决方案1】:

    您可以使用 UITabBarDelegate 方法检测选项卡何时被按下:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITabBarDelegate

    您可以让您的 UITabBarController 类成为委托并在实现中添加方法:

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
    { 
        NSLog(@"tab selected: %@", item.title); 
    } 
    

    【讨论】:

    • didSelectItem 方法看起来像是在实际选择项目之前和 shouldSelectViewController 之前调用的。当用户从更多表中选择一个项目时,它似乎不会被调用。
    • 如果 tabBar 由 UITabBarController 管理,您无法将 UITabBar 与您自己的委托连接
    【解决方案2】:

    How to get title of UITabBarItem in the More section?

    - (void)tabBarController:(UITabBarController *)tabBarController 
     didSelectViewController:(UIViewController *)viewController
    {
        NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
        NSLog(@"controller title: %@", viewController.title);
    
        if (viewController == tabBarController.moreNavigationController)
        {
            tabBarController.moreNavigationController.delegate = self;
        }
    }
    

    【讨论】:

    • 感谢 MacDev!我现在说得通了……我找了两个多小时都没找到这个问题!!!
    • 但它仍然没有调用“didSelectViewController”并生成警告。
    【解决方案3】:

    如果您使用的是标签栏控制器,您应该避免了解标签项目和视图控制器之间的映射——这是标签栏控制器的工作。如果您尝试将标签栏用于其他用途,那么您应该直接使用 UITabBar 而不是使用 UITabBarController。如果使用UITabBar,则可以将自己的对象设置为选项卡栏的委托,然后委托将在所选项目更改时获取消息。

    【讨论】:

      【解决方案4】:

      您可以使用UIViewController 中的以下代码访问所选项目的索引。它会总是返回标签的索引。

      self.tabBarController.selectedIndex;
      

      所以如果你有例如6 个项目,您可以转到“更多...”选项卡,选择您的“第 5 个”项目,selectedIndex 将为 4。如果您转到更多选项卡并选择第 6 项,它将返回 5


      编辑:如果你想检查一些 UITabBarItem 的当前位置,你可以这样做:

      首先,在您的 XIB 文件中,您应该编辑每个选项卡的 tag 属性,以便第一个选项卡的标签 = 100、2nd - 200、3rd - 300 等。

      然后在 ViewController 中添加这段代码:

      UIViewController *selectedVC = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
      int selectedItemTag = selectedVC.tabItem.tag;
      

      然后你可以通过使用selectedItemTag变量来确定它是什么viewController。在这种情况下,您可以通过以下方式确定 selectedIndex:selectedIndex = (selectedItemTag-100)/100

      tag 属性在自定义您的 UITabBar 时更改,因此您可以信任它们 :)

      【讨论】:

      • 你好卡希夫。您的方法听起来不错,但不能让用户重新排序标签栏项目?那么每一项的索引都会发生变化。
      • 那我很快就会编辑我的答案,因为我知道如何处理它。
      【解决方案5】:

      1. 因此,如果您使用的是 UITabBarController,您可以让该类实现 UITabBarControllerDelegate,并将您的 UITabBarController 委托设置为在 TabBar 时需要通知的类选定的项目发生变化,然后将委托方法添加到您的类:

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

      在此方法中,您可以使用 UITabBarController selectedIndex 属性来了解当前选择的索引:

      -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:      (UIViewController *)viewController
      {
          NSLog(@"Selected index: %d", tabBarController.selectedIndex);
      }
      

      2.如果您不只使用 UITabBar,您可以在此帖子中关注 Ken Pespisa 和 iCoder 在此帖子Ken Pespisa and iCoder 中的答案。 p>

      【讨论】:

        【解决方案6】:

        因为您将标签添加到您的 EVERY UITabBarItem(即使是索引为 5 或更多的标签)。

        您可以使用以下代码跟踪选择了哪个选项卡:

        //MARK: - UITabBarControllerDelegate
        
        func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        
            if viewController == tabBarController.moreNavigationController {
                tabBarController.moreNavigationController.delegate = self
            } else {
                setSelectedTabBarOption()
            }
        }
        
        
        //MARK: - UINavigationControllerDelegate
        
        func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
            setSelectedTabBarOption()
        }
        
        
        private func setSelectedTabBarOption() {
        
            if let viewControllers = viewControllers {
                let selectedController: UIViewController? = viewControllers.count > selectedIndex ? viewControllers[selectedIndex] : nil
                if let tag = selectedController?.tabBarItem.tag {
                    //do whatever with your tag
                }
            }
        }
        

        【讨论】:

          【解决方案7】:
          - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
          {
          
           NSLog(@"Selected index: %d", tabBarController.selectedIndex);
          
          if (viewController == tabBarController.moreNavigationController)
          {
              tabBarController.moreNavigationController.delegate = self;
          }
          
          NSUInteger selectedIndex = tabBarController.selectedIndex;
          
          switch (selectedIndex) {
          
              case 0:
                  NSLog(@"click tabitem %u",self.tabBarController.selectedIndex);
                  break;
              case 1:
                  NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
                  break;
          
              default:
                  break;
          
          }
          
          }
          

          【讨论】:

            猜你喜欢
            • 2016-02-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-08
            • 1970-01-01
            • 2010-12-20
            • 1970-01-01
            相关资源
            最近更新 更多