Search code examples
iphoneiosuitabbarcontrolleruitabbar

should not load tabbar item based on permission ios


I am trying to load a tabbar with multiple view, but I want few tabs opened only if the user has permission. Is there any delegate call that will handle this? I looked in tabbardelegate it only has didSelectItem unlike the textfield which has bool for return text so I choice to return or not.

Thanks


Solution

  • You can use the - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item you discussed.

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        if (item == 2 || item == 3) { //Tab 2 and 3 are protected
            if (!userHasPermission) {
                tabBar.selectedItem = 0; //Make user go to first tab if the user does not have permission.
            }
        }
    }
    

    Or if you want certain items disabled. In your -viewDidLoad

    if (!userHasPermission) {
        UITabBarItem *tabBarItem = [[myTabBar items] objectAtIndex:2];
        [tabBarItem setEnabled:NO];
    }