如何识别标签栏项目? [英] How to Identify tab bar items?

查看:92
本文介绍了如何识别标签栏项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何识别标签栏中的项目?

I would like to know how can I identify the items in the tab bar?

我有一个包含NAvigationController的tabBarController,如下所示:

I have a tabBarController that contain NAvigationController like this:

NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:6];

每个navigationController都在这个数组中。

Each navigationController is inside this array.

我使用以下方法管理每个标签栏项目中的操作:

I manage the actions in each tab bar item with the method:

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

我在这个方法中,即ie :

And I in this method, i.e.:

if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])

像这样我确定了我点击的标签栏项目。

Like this i identify wich tab bar item i click on.

但是问题是你可以在iphone屏幕上编辑Tabbar(因为数组中有6个viewControllers初始化tabbar)然后,我使用的方式不正确,因为我可以更改tabbar中viewcontrollers的位置当我使用这个编辑工具时。

BUT the problem is that you can edit the Tabbar in the iphone screen (because there are 6 viewControllers in the array that initialize the tabbar) and then, the way that i'm using is incorrect,because i can change the position of the viewcontrollers in the tabbar when i use this edit tool.

谢谢

推荐答案

你可以使用 UITabBarItem s标签属性为每个 UITabBarItem 提供唯一的数字标识符,然后进行比较。

You can use the UITabBarItem's tag property to give each UITabBarItem a unique numerical identifier, then compare that.

示例:

#define FirstViewController 1
#define SecondViewController 2
switch ([[viewController tabBarItem] tag]) {
  case FirstViewController:
    //the user selected your first view controller, no matter where it is on the tabbar
    break;
  case SecondViewController:
    break;
  ... etc
}

你可以记住每个人的指针 navigationControllers 并将这些与 viewController 参数进行比较。

You can remember pointers to each of your navigationControllers and compare those against the viewController parameter.

示例:

//during your initial setup of the tabBarController:
UIViewController * firstViewController = //The view controller in the first tab
UIViewController * secondViewController = //The view controller in the second tab

...

if (viewController == firstViewController) {
  ...
} else if (viewController == secondViewController) {
  ...
}

你可以禁止编辑你的 UITabBarController (传递一个空数组或 nil 到控制器的 customizableViewControllers 属性)。

You can disallow editing on your UITabBarController (pass an empty array or nil to the controller's customizableViewControllers property).

示例:

[myTabBarController setCustomizableViewControllers:nil];

这篇关于如何识别标签栏项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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