Search code examples
iosswiftxcodefirebaseviewcontroller

iOS on tab bar change


I want to know when the tab in the tab bar changes, so that i can report it to Firebase Analytics. How do i do this.

I tried this

override func viewDidAppear(_ animated: Bool) {
    Analytics.logEvent("projects_open", parameters: [:])
}

But i have a feeling that what would also run when i go back to it from another ViewController. I need something that can detect when a tab is opened, not when it becomes visible.

Is there another func that works for this?


Solution

  • Swift 3.0

    Use this two delegate methods, and don't forget to assign delegate to self.

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        //MARK: - UITabBarControllerDelegate
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
       //MARk: - UITabBarDelegate
    }