简体   繁体   中英

Select a tab bar item programmatically (not using UITabBarController)

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.

Again, I'm not using UITabBarController

Thanks

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

我认为这很快就会:

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

斯威夫特3:

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

In swift if tabbar is used not tabbarcontroller set default select

var tabbar:UITabBar?//if declare like this

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

or

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

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

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

Before select active tab bar item on viewDidLoad event

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM