2

I have created a storyboard with UITabbarController as initial rootViewController and added 6 items in tab bar

enter image description here

Till here it is fine. now i need to manage tab bar items on following conditions :

  1. if user not logged in - remove 6th item
  2. 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.

Sujay
  • 2,510
  • 2
  • 27
  • 47
  • what is not working ?Show the code related to deleting the items in tab bar! – Teja Nandamuri May 19 '16 at 14:15
  • @TejaNandamuri, sorry I missed my code while adding here, I have updated my question – Sujay May 19 '16 at 14:31
  • did u check the for loop gets executed ? – Teja Nandamuri May 19 '16 at 14:33
  • @TejaNandamuri, Yes it is executing. – Sujay May 19 '16 at 14:35
  • This question started out so strong, then turned into mush at the end with "not working, please assist". – danh May 19 '16 at 14:55
  • @danh, I tried to improve my question, please check the updated question. – Sujay May 19 '16 at 15:15
  • This line `NSMutableArray *tabbarItems = [NSMutableArray arrayWithArray:[tabbarController viewControllers]];` is puzzling, because (a) where is viewControllers defined an initialized, and (b) it looks like the tabBarController will be a tab bar item on itself. The way to improve your question is show what investigation you've done. e.g. Add NSLogs to the code and show the output in comments, e.g. simplify that logic that decides which vcs to add and remove, and just hardcode adding one or subtracting one. – danh May 19 '16 at 15:24
  • (a) `viewControllers` is property of `UITabBarController`. I could have use it like `tabbarController.viewControllers` (b) `tabbarController` is object of `UITabBarController` – Sujay May 19 '16 at 15:45
  • I see. I read that (incorrectly) as an array literal. In other words, `NSMutableArray *tabbarItems = [tabbarController.viewControllers mutableCopy]`. Anyway, as I suggest, just hardcode an add or remove to that array to see what happens – danh May 19 '16 at 16:04
  • @danh, I tried but not getting any result. – Sujay May 19 '16 at 16:34

0 Answers0