Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable/Enable tabs in UITabBarController in storyboard-based app

I've built my app with storyboards and all views are managed by a tabbarcontroller.

So on launch (I'm only working on the iPad UI currently) it does this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
        UISplitViewController *splitViewController = [tabBarController.viewControllers objectAtIndex:0];
        UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
        splitViewController.delegate = (id)navigationController.topViewController;

        UINavigationController *masterNavigationController = [splitViewController.viewControllers objectAtIndex:0];
        ProductionMasterViewController *controller = (ProductionMasterViewController *)masterNavigationController.topViewController;
        controller.managedObjectContext = self.managedObjectContext;
    }
}

I want to be able to enable or disable the tabs in the tabBarController based on user input (so, for example, an item needs to be selected in the first tab in order to access the second and third tabs, which are disabled by default)

What I'm not clear on is how to access the tabs in order to enable/disble them. Would I create an instance of the appdelegate and then do something like

AppDelegate *d = (AppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabs = (UITabBarController *)[d.window rootViewController];
[[[[tabs tabBar] items] objectAtIndex:2] setEnabled:YES];
[[[[tabs tabBar] items] objectAtIndex:3] setEnabled:YES];
[[[[tabs tabBar] items] objectAtIndex:4] setEnabled:YES];

(That kinda seems like it should work but it also seems fairly gross.)

like image 310
Charles Bandes Avatar asked Apr 19 '12 17:04

Charles Bandes


People also ask

How do I add a controller to my tab bar controller storyboard?

Open Main. storyboard and select the map view controller. From Xcode's Editor menu, choose Embed In → Tab Bar Controller. This will add the map view controller to the view controllers array of a new tab bar controller.

How do I add a tab bar to a storyboard?

Add a new Tab Bar ItemDrag View controller into the storyboard. Drag Tab Bar Item into the newly created view controller, it will sit at the same level of the View .

What is a tab bar view controller?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


1 Answers

Since you're using a Storyboard based app, I'd assume you have the UITabBarController defined in the storyboard as the root controller. Incidentally, you can also retrieve it by identifier instead of walking from the window to the root view controller.

Restricting which tabs are selectable, is achieved by setting a delegate of the UITabBarController (i.e. one that conforms to UITabBarControllerDelegate).

In the delegate, you can implement these two methods:

– tabBarController:shouldSelectViewController:

– tabBarController:didSelectViewController:

Likely, you just need the first to restrict (inhibit) selection, until your workflow is ready.

Another approach, is to set the "viewControllers' property on the tab bar controller, each time a milestone is passed. At each milestone, you set a more expansive array of view controllers into this property, which will open up the selection of the additional tab item.

SWIFT 3

(expanded for ease of understanding)

let arrayOfTabBarItems = tabBarController?.tabBar.items

        if let barItems = arrayOfTabBarItems, barItems.count > 0 {
            os_log("barItems.count is now ", barItems.count)
            tabBarItem0 = barItems[0]
            tabBarItem0.isEnabled = true
            tabBarItem1 = barItems[1]
            tabBarItem1.isEnabled = true
            tabBarItem2 = barItems[2]
            tabBarItem2.isEnabled = true
            tabBarItem3 = barItems[3]
            tabBarItem3.isEnabled = true
            tabBarItem4 = barItems[4]
            tabBarItem4.isEnabled = true
        }

This can be used in your viewWillAppear on each tab controller. Check your rules against this and restrict each tab accordingly. (more concise method)

let arrayOfAllTabBarItems = tabBarController?.viewControllers
    if   let tabBarArray = arrayOfAllTabBarItems, tabBarArray.count > 0 {
        for x in 0...tabBarArray.count-1 {
            let tabBarItem = tabBarArray[x]
            if tabBarItem.title != nil {
                if tabBarItem.title == "Tab1" || tabBarItem.title == "MyTab" || tabBarItem.title == "Tab2Check" {
                    tabBarItem.tabBarItem.isEnabled = !(isMyRuleTrue!)
                }
            }
        }
    }
like image 134
idStar Avatar answered Sep 30 '22 18:09

idStar