简体   繁体   English

以编程方式选择选项卡栏项(不使用UITabBarController)

[英]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.

相关问题 以编程方式创建UITabBarController时检索last(6th)选项卡栏项时崩溃 - Crash when retrieving last(6th) tab bar item when programmatically creating UITabBarController 检测UITabBarController的选项卡栏项目更改 - Detect Tab bar item change for UITabBarController 没有标签栏项目的UITabBarController中的UIViewController - UIViewController in a UITabBarController without tab bar item UITabBarController添加不带标签栏项目的viewcontroller - UITabBarController add viewcontroller without tab bar item 从UITabBarController场景中删除选项卡栏项 - Remove tab bar item from a UITabBarController scene UITabBarController以编程方式添加,但是标签栏不显示 - UITabBarController added programmatically, but tab bar doesn't display 如何在没有视图控制器的情况下将标签栏项添加到UITabBarController? - How to add a tab bar item to UITabBarController without a view controller? 共享按钮作为 UITabBarController 上的标签栏项目,没有关联的控制器 - share button as tab bar item on UITabBarController with no controller associated 将控制器添加到UITabBarController,而标签栏中没有出现新项目 - Add controller to UITabBarController without new item appearing in the tab bar UITabBarController最初高亮显示所有选项卡栏项目图像 - UITabBarController initially highlights all tab bar item images
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM