[英]Select a tab bar item programmatically (not using UITabBarController)
I have a view derived from UIViewControler (not UITabBarController). 我有一个从UIViewControler派生的视图(不是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. 我使用UITabBarDelegate允许视图在用户点击每个标签栏项时执行某些操作。
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 同样,我没有使用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 在swift中如果使用tabbar而不是tabbarcontroller设置默认选择
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 在viewDidLoad事件上选择活动标签栏项目之前
[self.tabBar setSelectedItem: [self.tabBar.items objectAtIndex:0]]; [self.tabBar setSelectedItem:[self.tabBar.items objectAtIndex:0]];
在Xamarin.ios中,我们可以像这样使用mainTabBarController.selectedIndex = 3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.