How to manage tab bar item in running app in ios

↘锁芯ラ 提交于 2019-12-13 16:17:14

问题


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 :

  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.

来源:https://stackoverflow.com/questions/37325818/how-to-manage-tab-bar-item-in-running-app-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!