Detect when a tab bar item is pressed

前端 未结 6 941
灰色年华
灰色年华 2020-11-28 07:37

I have a root view controller which isn’t set as the custom class for any of my view controllers on my storyboard. Instead, all of my view controllers are subclassing this c

6条回答
  •  一向
    一向 (楼主)
    2020-11-28 07:58

    Swift 5

    To detect if a specific TarBarItem has been selected I use this on my custom TabBarController:

        class MainTabBarController: UITabBarController, UITabBarControllerDelegate {
    
            override func viewDidLoad() {
                super.viewDidLoad()
    
                self.delegate = self
    
            }
    
    
       func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    
            let selectedIndex = tabBarController.viewControllers?.firstIndex(of: viewController)!
            if selectedIndex == 0 {
                print("first tab bar was selected") 
            } else {
                //do whatever
            }
        }
    
    }
    

提交回复
热议问题