如何在 swift 中正确显示选项卡控制器

How to correctly display a tab controller in swift

我正在为我的应用程序制作一个登录屏幕,一切都按预期工作,直到我尝试在登录后显示我的主视图(使用标签栏控制器)。

唯一的问题是它只显示标签栏上的第一项。我必须按其他按钮才能出现。

我正在使用此代码:

//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController

self.presentViewController(vc, animated: true, completion: nil)

我的猜测是我需要同时加载它们,但我不知道...

我最近就是这样做的。我将我的 tabBarController 和登录屏幕一起加载,一旦用户登录(或完成第一个屏幕体验),您就可以模态关闭控制器。

func showloginView() {

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
self.window?.makeKeyAndVisible()

    var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
    let nav = UINavigationController(rootViewController: viewController)
    self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
}

用于显示您的 tabBarController 和编辑任何 tabBarItems

let tabBarController = self.window?.rootViewController as? UITabBarController
let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
let nav = tabBarRootViewControllers[0] as? UINavigationController

希望这对您有所帮助 =)

如果你 link 一个 UIButton,你可以使用它打开一个 UITabBarController(假设你使用的是 Storyboard)

@IBAction func openTabBar(sender: AnyObject) {
        var tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController") as! UITabBarController
        self.presentViewController(tabBarController, animated: false, completion: nil)
    }

这将弹出当前视图并打开标签栏控制器。