views:

883

answers:

3

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

I have a tabBarController that contain NAvigationController like this:

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

Each navigationController is inside this array.


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

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

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.

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.

Thanks

+4  A: 

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

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.

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).

Example:

[myTabBarController setCustomizableViewControllers:nil];
Dave DeLong
1) If I give a UITabBarItem's tag to each UITabBarItem, I'm not associating this tabbaritem with the viewcontroller, rigth?, I mean, if i edit the tabbar, then i can find the tabaritem, but not the viewcontroller.2)How to do this?3)This will be my last option, because i would like to solve the problem, and allow edit the tab bar.The way that i'm comparing is like this:if (viewController == [self.tabBarController.viewControllers objectAtIndex:0])Should I change this way?Thanks
@Miriam Edited the answer w/ more information
Dave DeLong
A: 

But, I'm creating the ViewControllers like this: (then i cannot make #define, or put different names)

(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 {

// 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];

}

A: 

To do this, I started off with the Elements demo. 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
}
Greg Combs
But if you are in the same class that i explained you (where is declared each ViewController) you can not call "datasource.name" cos doesn't exist this.How can you call in this same class to make the comparison?Thanks
VideosTableViewController * tabbedViewController = (VideosTableViewController *)[[localViewControllersArray objectAtIndex:[WHATEVER]] topViewController];if (tabbedViewController.dataSource.name == @"Something") { // do something}
Greg Combs