以编程方式更改 tabBar 项目? [英] Programmatically change tabBar item?

查看:33
本文介绍了以编程方式更改 tabBar 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户登录或未登录时更改 TabBar Item.

I would like to change a TabBar Item, when a User is logged in or not.

例如:我有 5 个不同的 tabBar 项目,它们都创建了一个故事板.

For example: i have 5 different tabBar items, all created an Storyboard.

现在我想在我的用户没有帐户时更改索引为 2(或标签 == 2)的 tarBar.我想加载一个不同的 rootViewController.rootViewController 还不是我的 TabBar 的一个项目,我会加载一个完全不同的控制器.

Now i want to change the tarBar with index 2 (or tag == 2) when i user has no account. I would like to load a different rootViewController. The rootViewController is not already an item of my TabBar, i would load a totally different Controller.

这样做的最佳方法是什么?我可以简单地更改图标:

What is the best way to do this? I can simple change the icon with:

self.tabBar.items![0].selectedImage = UIImage(named: "icon_cal_grey")

但是如何更改 rootViewController?

But how to i change the rootViewController?

我应该在这里做吗?

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

    if item.tag == 1 {
        // ?
    }

}

或者应该创建一个 UINavigationController 作为 RootViewController,并在此处加载正确"的 ViewController 作为 RootViewController?

Or should ill create a UINavigationController as RootViewController, and load here the "correct" ViewController as RootViewController?

推荐答案

检查用户是否登录,然后更改 UITabBarController 的视图控制器:

Check if user is logged in and then change view controllers of UITabBarController:

tabbarController.viewControllers?.replaceRange()

这也可能有助于 在 Swift 中设置 UITabBarController 的视图控制器

这篇关于以编程方式更改 tabBar 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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