Reputation: 795
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.)
Upvotes: 5
Views: 6679
Reputation: 10814
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!)
}
}
}
}
Upvotes: 5