0

I have an application with a login system. I want the tabBarController to change dynamically at runtime if the user login himself successfully! I have 5 tabs (Accueil, Tous les Voyants, Inscription, Connexion, Aide).

When the user hit the login button, I want to change Inscription to Achat Jetons and Connexion to Profile and link another ViewController to both of theses tabBarItems!

Right now, I've successfully managed to replace the title and image logo of my tab bar. But I don't know how to link the viewControllers to them! Here's what I got right now:

- (IBAction)BTN_ConnexionClick:(id)sender {
UITabBarController *tabBarController = (UITabBarController *)self.tabBarController;
UITabBar *tabBar = tabBarController.tabBar;

UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:3];

[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"menu_iOS_achat.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"menu_iOS_achat.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"menu_iOS_profile.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"menu_iOS_profile.png"]];

tabBarItem1.title = @"Achat Jetons";
tabBarItem2.title = @"Profile";

}

I have created 2 new viewControllers via StoryBoard IB, I just don't know how to replace old linked viewController with new ones! Thanks for your help! :)

Shinnyx
  • 2,144
  • 2
  • 14
  • 21

1 Answers1

0

The mistake you're making is that you're changing the tab bar controller's tab bar's tab bar items directly. Don't! Change the tab bar controller's view controllers. The tab bar controller gets its tab bar items from them.

You might want to read my book on this topic:

http://www.apeth.com/iOSBook/ch19.html#_configuring_a_tab_bar_controller

Notice especially:

The tab bar controller’s tab bar will automatically display the tabBarItem of each child view controller

Do not do anything to mess that up! (You are messing it up.) Manipulate a view controller's tabBarItem. Manipulate a tab bar controller's viewControllers. Do not touch a tab bar controller's tab bar yourself.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Alright, I've put my title property and my image property in the concerned viewController itself. Now I have this in my login button action: UIViewController* profile = [SceneProfile new]; [self.tabBarController setViewControllers:@[profile]]; It does change my menu, but it displays a black window as content. (I've created my profile ViewController layout in the storyboard) Do you mind explaining me how I can get my layout to show up? Thank you matt! – Shinnyx Mar 25 '13 at 19:05
  • You've "created your profile ViewController layout in the storyboard." But `[SceneProfile new]` knows nothing of that. You need to use an identifier and fetch out the SceneProfile instance that you've drawn _in the storyboard_ (`instantiateViewControllerWithIdentifier:`). Either that or use a nib called "SceneProfile.xib", and not a storyboard at all. – matt Mar 25 '13 at 19:12
  • By the way, it might help you to read my book's chapter on view controllers: http://www.apeth.com/iOSBook/ch19.html. You will learn what a view controller is, where it gets its view, what a storyboard really is, how a tab bar controller and other parent view controllers work, and so on. Those seem to be the things you don't know about here. – matt Mar 25 '13 at 19:14
  • I'm gonna read that chapter, thanks for pointing me the right direction! – Shinnyx Mar 25 '13 at 19:40