在基于故事板的应用程序中禁用/启用UITabBarController中的选项卡 [英] Disable/Enable tabs in UITabBarController in storyboard-based app

查看:126
本文介绍了在基于故事板的应用程序中禁用/启用UITabBarController中的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用故事板构建了我的应用程序,所有视图都由tabbarcontroller管理。



所以在发布时(我目前只在iPad上工作),它会这样做:

   - (BOOL)应用程序:(UIApplication *)应用程序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;
}
}

我希望能够启用或禁用标签在tabBarController中基于用户输入(因此,例如,需要在第一个选项卡中选择一个项目才能访问第二个和第三个选项卡,默认情况下禁用这些选项卡)



我不清楚的是如何访问选项卡以启用/解除它们。我是否会创建appdelegate的实例,然后执行类似

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

(这似乎应该有效,但看起来也相当糟糕。)

解决方案

由于您使用的是基于Storyboard的应用程序,我假设您拥有 UITabBarController 在故事板中定义为根控制器。顺便提一下,您也可以通过标识符检索它,而不是从窗口走到根视图控制器。



通过设置一个委托来实现限制哪些选项卡是可选的。 UITabBarController(即一个符合 UITabBarControllerDelegate )。



在委托中,您可以实现以下两种方法: / p>

- tabBarController:shouldSelectViewController:



- tabBarController:didSelectViewController:



可能只需要第一个限制(禁止)选择,直到您的工作流程准备就绪。



另一种方法是在每次里程碑传递时在标签栏控制器上设置viewControllers属性。在每个里程碑,你设置一个更广泛的数组将控制器查看到此属性,这将打开选择附加标签项。



SWIFT 3



(例如为了便于理解而摇摇欲坠)

 让arrayOfTabBarItems = tabBarController?.tabBar.items 

如果让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可以在每个选项卡控制器上的viewWillAppear中使用。检查您的规则并相应地限制每个选项卡。
(更简洁的方法)

 让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!)
}
}
}
}


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.)

解决方案

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!)
                }
            }
        }
    }

这篇关于在基于故事板的应用程序中禁用/启用UITabBarController中的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆