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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29286683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:21:47  来源:igfitidea点击:

Select a tab bar item programmatically (not using UITabBarController)

iosuitabbaruitabbaritem

提问by tala9999

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.

我有一个派生自 UIViewControler(不是 UITabBarController)的视图。在这个视图中,我添加了一个带有多个标签栏项目的标签栏。我使用了 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

谢谢

回答by Foster Bass

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

Which in swift, I think would be:

很快,我认为是:

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

回答by Aliens

Swift 3:

斯威夫特 3:

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

回答by Ravi Kumar

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

回答by Monir Khlaf

if you inside UITabBarController you can useself.selectedIndex = // set target index

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

回答by Anisetti Nagendra

In Xamarin.ios, we can use like this mainTabBarController.selectedIndex=3;

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

回答by Andres Ortiz

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]];