ios – 在基于故事板的应用程序中禁用启用UITabBarController中的选项卡

ios – 在基于故事板的应用程序中禁用启用UITabBarController中的选项卡,第1张

概述我用故事板构建了我的应用程序,所有视图都由tabbarcontroller管理. 所以在发布时(我目前只在iPad UI上工作)它会这样做: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if ([[UIDevice 我用故事板构建了我的应用程序,所有视图都由tabbarcontroller管理.

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

- (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;    }}

我希望能够根据用户输入启用禁用tabbarController中的选项卡(例如,需要在第一个选项卡中选择一个项目才能访问第二个和第三个选项卡,默认情况下禁用这些选项卡)

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

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];

(这似乎应该有用,但看起来也相当粗糙.)

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

通过设置UITabbarController的委托(即符合UITabbarControllerDelegate的委托)来限制可选择哪些选项卡.

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

– tabbarController:shouldSelectVIEwController:

– tabbarController:dIDSelectVIEwController:

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

另一种方法是在每次里程碑传递时在标签栏控制器上设置“vIEwControllers”属性.在每个里程碑,您将一个更广泛的视图控制器数组设置到此属性中,这将打开选择的附加标签项.

SWIFT 3

(为便于理解而扩展)

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        }

这可以在每个选项卡控制器上的vIEwWillAppear中使用.检查您的规则并相应地限制每个选项卡.
(更简洁的方法)

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

以上是内存溢出为你收集整理的ios – 在基于故事板的应用程序中禁用/启用UITabBarController中的选项卡全部内容,希望文章能够帮你解决ios – 在基于故事板的应用程序中禁用/启用UITabBarController中的选项卡所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1068437.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存