2009-08-15 7 views
1

Je voudrais savoir comment puis-je identifier les éléments dans la barre d'onglets?Comment identifier les éléments de la barre d'onglets?

J'ai un tabBarController qui contiennent NavigationController comme ceci:

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

Chaque NavigationController se trouve dans ce tableau.


que je gère les actions dans chaque élément de la barre d'onglet avec la méthode:

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

Et je dans cette méthode, à savoir:

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

Vous aimez cette i identifier onglet Wich élément de la barre je clique sur. MAIS le problème est que vous pouvez modifier la barre d'onglets dans l'écran de l'iphone (parce qu'il y a 6 viewControllers dans le tableau qui initialisent la barre d'onglets) et ensuite, la façon dont j'utilise est incorrecte, car je peux changer le position des viewcontrollers dans la barre d'onglets lorsque j'utilise cet outil d'édition.

Merci

Répondre

6

Vous pouvez utiliser la propriété d » étiquette UITabBarItem pour donner à chaque UITabBarItem un identifiant numérique unique, puis les comparer.

Exemple:

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

Vous pouvez rappeler des pointeurs vers chacun de vos navigationControllers et comparer celles contre le paramètre viewController.

Exemple:

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

Vous pouvez interdire le montage sur votre UITabBarController (passer un tableau vide ou nil-customizableViewControllers propriété du contrôleur).

Exemple:

[myTabBarController setCustomizableViewControllers:nil]; 
+0

1) Si je donne l'étiquette d'un UITabBarItem à chaque UITabBarItem , Je n'associe pas cette tabbaritem avec le viewcontroller, rigth ?, Je veux dire, si je modifie la barre d'onglets, alors je peux trouver le tabaritem, mais pas le viewcontroller. 2) Comment faire? 3) Ce sera ma dernière option, car je voudrais résoudre le problème, et permettre de modifier la barre d'onglets. La façon dont je compare est la suivante: if (viewController == [self.tabBarController.viewControllers objectAtIndex: 0]) Dois-je changer de cette façon? Merci –

+0

@Miriam Édité la réponse w/plus d'informations –

0

Mais, je crée les ViewControllers comme ceci: (alors je ne peux pas faire #define, ou mettre des noms différents)

(UINavigationController *) createNavigationControllerWrappingViewControllerForDataSourceOfClass: (classe) 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]; 

}

0

Pour ce faire, je commencé avec la démo Elements. Dans cette démo, chaque source de données a son propre nom substitué.Ensuite, chaque fois que je besoin de faire quelque chose pour un onglet spécifique (car il a une autre source de données que d'autres onglets), dans mon contrôleur principal de navigation, je fais:

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

Mais si vous êtes dans la même classe que je vous ai expliqué (où est déclaré chaque ViewController), vous ne pouvez pas appeler "datasource.name" cos n'existe pas cela. Comment pouvez-vous appeler dans cette même classe pour faire la comparaison? Merci –

+0

VidéosTableViewController * tabbedViewController = (VideosTableViewController *) [[localViewControllersArray objectAtIndex: [WHATEVER]] topViewController]; if (tabbedViewController.dataSource.name == @ "Quelque chose") { // faire quelque chose } –

Questions connexes