简体   繁体   English

如何识别标签栏项目?

[英]How to Identify tab bar items?

I would like to know how can I identify the items in the tab bar? 我想知道如何识别标签栏中的项目?

I have a tabBarController that contain NAvigationController like this: 我有一个包含NAvigationController的tabBarController,如下所示:

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

Each navigationController is inside this array. 每个navigationController都在此数组内。


I manage the actions in each tab bar item with the method: 我使用以下方法管理每个标签栏项中的操作:

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

And I in this method, ie: 而我在这种方法中,即:

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

Like this i identify wich tab bar item i click on. 像这样,我确定我单击的最上面的标签栏项目。

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. 但是问题是您可以在iPhone屏幕上编辑Tabbar(因为数组中有6个初始化Tabbar的viewControllers),然后,我使用的方式不正确,因为我可以更改ViewController的位置当我使用此编辑工具时,在标签栏中。

Thanks 谢谢

You can use the UITabBarItem 's tag property to give each UITabBarItem a unique numerical identifier, then compare that. 您可以使用UITabBarItem的tag属性为每个UITabBarItem赋予唯一的数字标识符,然后进行比较。

Example: 例:

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

You can remember pointers to each of your navigationControllers and compare those against the viewController parameter. 您可以记住指向每个navigationControllers指针,并将其与viewController参数进行比较。

Example: 例:

//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) {
  ...
}

You can disallow editing on your UITabBarController (pass an empty array or nil to the controller's customizableViewControllers property). 您可以禁止在UITabBarController上进行编辑(将空数组或nil传递给控制器​​的customizableViewControllers属性)。

Example: 例:

[myTabBarController setCustomizableViewControllers:nil];

But, I'm creating the ViewControllers like this: (then i cannot make #define, or put different names) 但是,我正在像这样创建ViewController :(然后,我无法进行#define或放置其他名称)

(UINavigationController *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass { (UINavigationController *)createNavigationControllerWrappingViewControllerForDataSourceOfClass:(Class)datasourceClass {

id<VideosDataSource,UITableViewDataSource> dataSource = [[datasourceClass alloc] init];

// create the VideosTableViewController and set the datasource
VideosTableViewController *theViewController;   
theViewController = [[VideosTableViewController alloc] initWithDataSource:dataSource];

// create the navigation controller with the view controller
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];

// before we return we can release the dataSource (it is now managed by the ElementsTableViewController instance
[dataSource release];

// and we can release the viewController because it is managed by the navigation controller
[theViewController release];

return theNavigationController;

} }

(void)setupPortraitUserInterface { (void)setupPortraitUserInterface {

// a local navigation variable
// this is reused several times
UINavigationController *localNavigationController;

// Create a tabbar controller and an array to contain the view controllers
tabBarController = [[UITabBarController alloc] init];

// define a custom frame size for the entire tab bar controller that will be in the
// bottom half of the screen.
CGRect tabBarFrame;
tabBarFrame = CGRectMake(0, 0, 320, 460);
tabBarController.view.frame = tabBarFrame;

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

// setup the 6 view controllers for the different data representations

// create the view controller and datasource for the VideosSortedBySuggestionsDataSource
// wrap it in a UINavigationController, and add that navigationController to the 
// viewControllersArray array

localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySuggestionsDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];


// repeat the process for the VideosSortedBySuggSearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySuggSearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];            


// repeat the process for the VideosSortedByMostViewedDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedByMostViewedDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];


// repeat the process for the VideosSortedByTopRatedDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedByTopRatedDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];


// repeat the process for the VideosSortedBySearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];


// repeat the process for the VideosSortedBySearchDataSource
localNavigationController = [self createNavigationControllerWrappingViewControllerForDataSourceOfClass:[VideosSortedBySearchDataSource class]];
[localViewControllersArray addObject:localNavigationController];

// the localNavigationController data is now retained by the application delegate
// so we can release the local variable
[localNavigationController release];


// set the tab bar controller view controller array to the localViewControllersArray
tabBarController.viewControllers = localViewControllersArray;
// the localViewControllersArray data is now retained by the tabBarController
// so we can release this version
[localViewControllersArray release];
// set the window subview as the tab bar controller
[self.view addSubview:tabBarController.view];

} }

To do this, I started off with the Elements demo. 为此,我从Elements演示开始。 In that demo, each datasource has it's own overridden name. 在该演示中,每个数据源都有其自己的覆盖名称。 Then any time I need to do something for a specific tab (because it's got a different datasource than other tabs), in my main navigation controller, I do: 然后,每当我需要为特定选项卡做一些事情(因为它具有与其他选项卡不同的数据源)时,在我的主导航控制器中,我会执行以下操作:

if (datasource.name == @"Some name") {
    // something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM