iPhone应用程序 - 检测按下了哪个标签栏项目

时间:2011-05-04 15:12:02

标签: objective-c tabbar uitabbaritem

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

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

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

提前谢谢。

7 个答案:

答案 0 :(得分:21)

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

答案 1 :(得分:15)

您可以使用UIViewController中的以下代码访问所选项目的索引。它将始终返回选项卡的索引。

self.tabBarController.selectedIndex;

所以如果你有例如6项您可以转到“更多...”标签,选择“第5”项,selectedIndex将 4 。如果您转到更多标签并选择第6项,则会返回 5


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

首先,在您的XIB文件中,您应该编辑每个标签的tag属性,以便第一个标签的标签= 100,第2 - 200,第3 - 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属性已更改,因此您可以信任它们:)

答案 2 :(得分: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); 
} 

答案 3 :(得分:3)

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 and iCoder中按照Ken Pespisa和iCoder的回答。

答案 4 :(得分:2)

由于您向 EVERY "!aNULL"添加了标签(即使是索引为5及以上的标签)。

您可以使用以下代码跟踪选择的标签:

UITabBarItem

答案 5 :(得分: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;

}

}

答案 6 :(得分:1)

如果您正在使用标签栏控制器,则应该避免了解标签项和视图控制器之间的映射 - 这是标签栏控制器的工作。如果你试图使用标签栏来做其他事情,那么你应该直接使用UITabBar而不是使用UITabBarController。如果您使用UITabBar,则可以将自己的对象设置为标签栏的委托,然后只要所选项目发生更改,委托就会收到消息。