2010-10-11 4 views
2

Dans iOS, la propriété TabBar dans TabBarController est en lecture seule. Comment puis-je associer un élément personnalisé à un contrôleur de vue particulier? Comment puis-je accéder à UITabBarItems à l'intérieur du tabBar?Définir UITabBarItem personnalisé par programme?

Vous aimez cette

CustomView *custom = [[CustomView alloc] init]; 
UITabBarItem *customTab = [[UITabBarItem alloc] initWithTitle:@"Custom" image:[UIImage imageNamed:@"custom.png"] tag:0]; 
SecondView *second = [[SecondView alloc] init]; 
UITabBarItem *secondTab = [[UITabBarItem alloc] initWithTitle:@"Next" image:[UIImage imageNamed:@"next.png"] tag:1]; 
NSArray *views = [NSArray arrayWithObjects:custom,second,nil]; 
[tabBarController setViewControllers:views]; 
//how do I set the individual TabBarItems (customTab,secondTab) to be associated 
//with the views in question? tabBarController.tabBar is read only 
+0

Essayer de modifier tabBar avec tabBar: setItems: animated est un non-no. *** Termination de l'application en raison d'une exception non interceptée 'NSInternalInconsistencyException', raison: 'La modification directe d'une barre d'onglets gérée par un contrôleur de barre d'onglets n'est pas autorisée.' – Justin

Répondre

5

l'intérieur de chaque contrôleur de vue, vous pouvez définir une propriété tabBarItem. Si le contrôleur de vues appartient à un UITabBarViewController, l'élément associé sur la barre d'onglets sera mis à jour en conséquence.

Quelque chose comme ça

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    UITabBarItem *tbi = [[UITabBarItem alloc] initWithTitle:yourTitle image:yourIcon tag:yourTag]; 
    [self setTabBarItem:tbi] 
    [tbi release]; 
} 

Vous n'êtes pas limité à effectuer cette opération dans la méthode viewDidLoad, évidemment.

+0

Merci! Apple doit faire cette référence dans les documents de la référence UITabBar. – Justin

Questions connexes