以编程方式选择一个标签栏项目(不使用 UITabBarController)

问题描述:

我有一个派生自 UIViewControler(而不是 UITabBarController)的视图.在这个视图中,我添加了一个带有多个标签栏项目的标签栏.我使用了 UITabBarDelegate 来允许视图在用户点击每个标签栏项目时做一些事情.

I have a view derived from UIViewControler (not UITabBarController). In this view I added a tab bar with several tab bar items. I used the UITabBarDelegate to allow the view to do something when users tap on each tab bar item.

class MyViewController: UIViewController, UITabBarDelegate {

    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem!) {
        // do something
    }
}

我的问题是我们如何在第一次加载视图时以编程方式选择第一个标签栏项目?请注意,我还希望第一个选项卡项也处于活动"状态.

My question is how we can programmatically select the first tab bar item when the view is first loaded? Note that I would want the first tab item to be in "active" state also.

再说一次,我没有使用 UITabBarController

Again, I'm not using UITabBarController

谢谢

[tabBar setSelectedItem: [tabBar.items objectAtIndex:0]];

我认为很快:

tabBar.selectedItem = tabBar.items![0] as UITabBarItem