UITabBar(Controller) - 获取索引索引? [英] UITabBar(Controller) - Get index of tapped?

查看:724
本文介绍了UITabBar(Controller) - 获取索引索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标签栏应用程序,我需要知道用户在标签栏上点击什么按钮来显示相应的通知等。

I've got a tab bar application and I need to know when and what button a user taps on the tab bar as to display the appropriate notifications and such.

总之:我如何检测UITabBar上的UITabBarItem的索引?

提前感谢!

推荐答案

答案取决于UITabBar是否由UITabBarController管理。

The answer depends on whether or not the UITabBar is managed by a UITabBarController or not.

案例1 - UITabBar已由UITabBarController处理

实施 UITabBarControllerDelegate 协议。特别是 tabBarContoller:didSelectViewController:方法。将实现协议的类的实例设置为 UITabBarController 委托

Implement the UITabBarControllerDelegate protocol. Specifically the tabBarContoller:didSelectViewController: method. Set an instance of your class that implements the protocol as the delegate of the UITabBarController.

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController {
    NSUInteger indexOfTab = [theTabBarController.viewControllers indexOfObject:viewController];
    NSLog(@"Tab index = %u (%u)", (int)indexOfTab);
}

在这种情况下,您必须了解特殊情况,控制器中,以使更多选项卡显示。在这种情况下,您将收到一个调用 tabBarController:didSelectViewController:与不在列表中的视图控制器(它是一个内部UIKit类的一个实例UIMoreNavigationController) 。在这种情况下,我的示例中的 indexOfTab 将是 NSNotFound

In this case you have to be aware of the special situation where you have enough controllers in the tab controller to cause the "More" tab to be displayed. In that case you'll receive a call to the tabBarController:didSelectViewController: with a view controller that isn't in the list (it's an instance of an internal UIKit class UIMoreNavigationController). In that case the indexOfTab in my sample will be NSNotFound.

案例2 - UITabBar尚未由UITabBarController处理

实施 UITabBarDelegate 协议。特别是 tabBar:didSelectItem:方法。将实现协议的类的实例设置为 UITabBar 委托

Implement the UITabBarDelegate protocol. Specifically the tabBar:didSelectItem: method. Set an instance of your class that implements the protocol as the delegate of the UITabBar.

- (void)tabBar:(UITabBar *)theTabBar didSelectItem:(UITabBarItem *)item {
    NSUInteger indexOfTab = [[theTabBar items] indexOfObject:item];
    NSLog(@"Tab index = %u", (int)indexOfTab);
}

编辑:修改方法参数变量以消除OP的编译警告c $ c> tabBarController 被隐藏。

Modified the method parameter variables to eliminate the OP's compilation warning about tabBarController being hidden.

这篇关于UITabBar(Controller) - 获取索引索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆