18

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

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

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

先感谢您。

4

7 回答 7

21

如何在更多部分中获取 UITabBarItem 的标题?

- (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;
    }
}
于 2011-05-04T15:19:56.133 回答
15

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

self.tabBarController.selectedIndex;

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


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

首先,在您的 XIB 文件中,您应该编辑tag每个选项卡的属性,以便第一个选项卡的 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

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

于 2011-05-04T15:25:23.357 回答
5

您可以使用 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); 
} 
于 2011-05-04T15:16:14.337 回答
4

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和 iCoder在本文中。

于 2014-08-06T06:44:20.423 回答
2

由于您将标签添加到您的每一个 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
        }
    }
}
于 2015-07-22T10:24:20.237 回答
2
- (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;

}

}
于 2015-12-03T12:40:54.403 回答
1

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

于 2011-05-04T15:20:32.787 回答