Select 以编程方式创建标签栏项(不使用 UITabBarController)

Select a tab bar item programmatically (not using UITabBarController)

我有一个派生自 UIViewControler(不是 UITabBarController)的视图。在此视图中,我添加了一个带有多个选项卡栏项目的选项卡栏。我使用 UITabBarDelegate 允许视图在用户点击每个选项卡栏项目时执行某些操作。

class MyViewController: UIViewController, UITabBarDelegate {

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

我的问题是我们如何以编程方式 select 首次加载视图时的第一个选项卡栏项目?请注意,我希望第一个选项卡项也处于 "active" 状态。

同样,我没有使用 UITabBarController

谢谢

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

在 swift 中,我认为是:

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

在swift中如果使用tabbar而不是tabbarcontroller设置默认值select

var tabbar:UITabBar?//if declare like this

tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem

let tabbar = UITabBar()//if declare and initilize like this

tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem

在 viewDidLoad 事件 select 活动标签栏项目之前

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

Swift3:

tabBarController.selectedIndex = 0 // (or any other existing index)

如果你在 UITabBarController 中,你可以使用self.selectedIndex = // set target index

在Xamarin.ios中,我们可以这样使用 mainTabBarController.selectedIndex=3;