簡體   English   中英

以編程方式選擇選項卡欄項(不使用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
    }
}

我的問題是我們如何在首次加載視圖時以編程方式選擇第一個標簽欄項? 請注意,我希望第一個標簽項也處於“活動”狀態。

同樣,我沒有使用UITabBarController

謝謝

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

我認為這很快就會:

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

斯威夫特3:

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

在swift中如果使用tabbar而不是tabbarcontroller設置默認選擇

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

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

在viewDidLoad事件上選擇活動標簽欄項目之前

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

在Xamarin.ios中,我們可以像這樣使用mainTabBarController.selectedIndex = 3;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM