Objective c iphone应用程序-检测按下了哪个选项卡栏项

Objective c iphone应用程序-检测按下了哪个选项卡栏项,objective-c,tabbar,uitabbaritem,Objective C,Tabbar,Uitabbaritem,我有一个基于tab bar的应用程序,有5个以上的tab bar项目,因此我可以在视图中直接显示其中的4个项目,其余的可以通过选择“更多”选项卡获得。当按下选项卡栏项目时,我想检测它是哪一个。 因此,在 -(void)tabBarController:(UITabBarController*)tabBarCtrl didSelectViewController:(UIViewController*)viewController方法,我使用tabBarCtrl.selectedViewContro

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

这适用于视图中可见的选项卡(即前4个选项卡和“更多”选项卡),但不适用于按下“更多”选项卡后列表中显示的其余选项卡栏项目

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


提前感谢。

您可以使用UITabBarDelegate方法检测何时按下了选项卡:

您可以将UITabBarController类设置为委托,并在实现中添加方法:

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


如果您使用的是选项卡栏控制器,那么应该避免了解选项卡项和视图控制器之间的映射——这是选项卡栏控制器的工作。如果您试图为其他内容使用选项卡栏,则应直接使用UITabBar,而不是使用UITabBarController。如果使用UITabBar,您可以将自己的对象设置为选项卡栏的代理,然后每当所选项目发生更改时,代理将收到消息。

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

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


编辑:如果要检查某些UITabritem的当前位置,可以执行以下操作:

首先,在您的XIB文件中,您应该编辑每个选项卡的
tag
属性,以便第一个选项卡的tag=100、第二个-200、第三个-300等

然后在ViewController中添加以下代码:

UIViewController *selectedVC = [self.tabBarController.viewControllers objectAtIndex:self.tabBarController.selectedIndex];
int selectedItemTag = selectedVC.tabItem.tag;
然后,您可以使用
selectedItemTag
变量确定它是什么viewController。在这种情况下,您可以通过执行以下操作来确定selectedIndex:
selectedIndex=(selectedItemTag-100)/100


自定义UITabBar时,
标记
属性未更改,因此,您可以信任它们:)

1。因此,如果您使用的是UITabBarController,则可以使类实现UITabBarController delegate,并将UITabBarController委托设置为当选项卡栏所选项更改时需要通知的类,然后将委托方法添加到类中:

-(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的答案来回答。

因为你在每个
uitabaritem
中添加了标签(即使是索引为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
        }
    }
}

你好,卡西夫。您的方法听起来不错,但是用户不能重新排序选项卡栏项目吗?然后每个项目的索引都会改变。然后我会很快编辑我的答案,因为我知道如何处理它。谢谢MacDev!我现在明白了。。。我已经找了两个多小时了,但是没有找到这个问题!!!但是它仍然没有调用“didSelectViewController”并生成警告。didSelectItem方法看起来像是在实际选择项之前和shouldSelectViewController之前被调用的。当用户从“更多”表中选择项目时,它似乎不会被调用。如果选项卡栏由UITabBarController管理,则无法将UITabBar与您自己的代理连接
//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
        }
    }
}
- (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;

}

}