I have created a storyboard with UITabbarController
as initial rootViewController
and added 6 items in tab bar
Till here it is fine. now i need to manage tab bar items on following conditions :
- if user not logged in - remove 6th item
- if user logged in - remove 5th item
I am able remove tab item in didFinishLaunchingWithOptions
in AppDelegate
by following code
- (BOOL)application : (UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
NSString *getUserLoggedStatus = [[NSUserDefaults standardUserDefaults] valueForKey : @"USER_STATUS"];
BOOL loggedStatus = ([getUserLoggedStatus isEqualToString : @"1"]) ? true : false;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName : @"Main" bundle : nil];
UITabBarController *tabbarController = [storyboard instantiateViewControllerWithIdentifier : @"MyTabBarController"];
NSMutableArray *tabbarItems = [NSMutableArray arrayWithArray : tabbarController.viewControllers];
if (!loggedStatus) {
for (UINavigationController *nav in tabbarItems) {
if([nav.restorationIdentifier isEqualToString : @"CommunityStoryboardID"]) {
[tabbarItems removeObject : nav];
break;
}
}
[tabbarController setViewControllers : [NSArray arrayWithArray : tabbarItems]];
} else {
for (UINavigationController *nav in tabbarItems) {
if([nav.restorationIdentifier isEqualToString : @"RegisterStoryboardID"]) {
[tabbarItems removeObject : nav];
break;
}
}
[tabbarController setViewControllers : [NSArray arrayWithArray : tabbarItems]];
}
self.window.rootViewController = tabbarController;
return YES;
}
but I need to do the same when user register and login using 5th tab
on successfully login I am changing tab index to 0 and added following code in viewDidLoad
in HomeViewController
on condition that user is already logged-in and redirected from Register/Login page
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbarController = [storyboard instantiateViewControllerWithIdentifier : @"MyTabBarController"];
NSMutableArray *tabbarItems = [NSMutableArray arrayWithArray : tabbarController.viewControllers];
BOOL communityFlag = false;
for (UINavigationController *nav in tabbarItems) {
if([nav.restorationIdentifier isEqualToString : @"RegisterStoryboardID"]) {
[tabbarItems removeObject : nav];
break;
}
if([nav.restorationIdentifier isEqualToString:@"CommunityStoryboardID"]) {
communityFlag = true;
break;
}
}
if(!communityFlag) {
UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"CommunityStoryboardID"];
[tabbarItems addObject : controller];
}
[self.tabBarController setViewControllers:[NSArray arrayWithArray:tabbarItems]];
above code is not working and not throwing any error. please assist.