簡體   English   中英

檢查用戶是否在 UITabBarController 中選擇了相同的選項卡

[英]Check that user selected the same tab in UITabBarController

我有一個UITabBarController的子類。 問題是:有沒有辦法檢查用戶是否選擇了已經選擇的UITabBarItem

我已經為selectedIndexselectedViewController嘗試didSet以及不同的委托方法:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)

但是只有當它是不同的選項卡時才會調用它們(並且根本不調用selectedIndex

我需要它,例如,如果我想將當前打開的控制器滾動到頂部,如果它不是當前打開的控制器,那么我不想滾動它,我只想顯示它(以及其他一些東西與動畫有關)。

謝謝

編輯我的代碼還有另一個問題,它阻止方法override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)被調用。 就像在一個清晰的“新鮮”項目中一樣,它按預期工作。 謝謝

這是一個可以幫助您的快速方法。

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    let currentIndex = tabBar.items?.firstIndex(of: item)
    if selectedIndex == 0 && currentIndex == 0 {
        // Do Something
    } else if selectedIndex == 1 && currentIndex == 1 {
        // Do Something
    }
}

暫無
暫無

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

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