How to hide the tab bar item?

Go To


I am new to this iphone development.I have created a tab bar application which consist of 6 tabs this is the code for the tab bar controller creation in appdelegaate file didfinishlaunching

UIViewController *viewController1 = [[[cardsAvailable1 alloc] 
                                      initWithNibName:@"cardsAvailable1" bundle:nil] autorelease];
UIViewController *viewController2 = [[[fetchcard1 alloc] 
                                      initWithNibName:@"fetchcard1" bundle:nil] autorelease];
UIViewController *viewController3 = [[[registration alloc] 
                                      initWithNibName:@"registration" bundle:nil] autorelease];
UIViewController *viewController4 = [[[logintab alloc] 
                                      initWithNibName:@"logintab" bundle:nil] autorelease];

UIViewController *viewController5 = [[[registration alloc] 
                                      initWithNibName:@"logout" bundle:nil] autorelease];
UIViewController *viewController6 = [[[logintab alloc] 
                                      initWithNibName:@"myprofile" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

self.tabBarController.viewControllers = [NSArray arrayWithObjects:
                                         [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease], 
                                         [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:viewController3] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:viewController4] autorelease], 
                                         [[[UINavigationController alloc] initWithRootViewController:viewController5] autorelease],
                                         [[[UINavigationController alloc] initWithRootViewController:viewController6] autorelease],
 self.tabBarController.selectedIndex = 3;

self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.tabBarController.view];

[self.window makeKeyAndVisible];

now my problem is after a person is login ie onclick the login button present in the login page i would like to hide the two tab bar item ie registration page and also login page and need to bring the logout page and myprofile page with the tab bar including fetch card and card avalable can any one suggest me a method to do that?

2012-04-04 04:50
by user1288402


You can add and remove items on a tab bar by editing the array of view controllers for the tab bar.

NSMutableArray newArrayOfItems = [[NSMutableArray alloc] initWithArray:self.tabBarController items]];
[newArrayOfItems removeObjectAtIndex:indexOfUnneededItem];
[self.tabBarController setItems:newArrayOfItems animated:true];
[newArrayOfItems release];

In your example and in response to your comment the following code will work as long as you import your app delegate header.

NSMutableArray newArrayOfItems = [[NSMutableArray alloc] initWithArray: [[[UIApplication sharedApplication] delegate].tabBarController items]];
[newArrayOfItems removeObjectAtIndex:indexOfUnneededItem];
[[[UIApplication sharedApplication] delegate].tabBarController setItems:newArrayOfItems animated:true];
[newArrayOfItems release];
2012-04-04 04:58
by Kyle Richter
i think this wont work since i am creating tab bar application so the tab bar controller code is written in delegate file didfinishlaunching.then how can i use this code in another class file.Can u tell me where to put this code - user1288402 2012-04-04 05:03
You will need a reference to the tab bar controller in your additional class files. You could for example make the tab bar controller a property of the app delegate and access it there. Another approach would be to add a method in the app delegate that your class can perform and have it remove the tab - Kyle Richter 2012-04-04 05:13
I have updated the code in my reply to work for your particular app - Kyle Richter 2012-04-04 05:17
thanks let me try thi - user1288402 2012-04-04 05:24


You can set the hidesBottomBarWhenPushed property before pushing view controller. There is the sample code below:

LoginController *lc = [[LoginController alloc] initWithNibName:nil bundle:nil];
lc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:lc animated:YES];
[lc release];
2012-04-04 08:22
by tangqiaoboy