2

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?

alvarlagerlof
  • 1,516
  • 1
  • 18
  • 27

4 Answers4

4

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
}
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
0

There is a delegate function on UITabbarController for detecting that a tab was selected:

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

You can also access the selected index like this:

tabController.selectedIndex
bughana
  • 324
  • 5
  • 12
  • Do I need to add one on each tab or can i put it in the AppDelegate? – alvarlagerlof May 27 '17 at 10:11
  • Is your tabbar controller created in the app delegate? If yes then you first need to assign the delegate property (tabbarController.delegate = self) and then implement this function in the appdelegate. It will be called each time the user switches tabs. Here is the documentation for the delegate protocol: https://developer.apple.com/reference/uikit/uitabbarcontrollerdelegate – bughana May 27 '17 at 10:13
  • I made it in my storyboard, so i guess i but one in each tab – alvarlagerlof May 27 '17 at 10:15
  • Ok, you can also assign the delegate in your storyboard or by creating an outlet and then assign it in code. If it is your rootviewcontroller you can just access it in the app delegate like this: let tab = window?.rootViewController as! UITabBarController – bughana May 27 '17 at 10:17
0

If you use only tabBar.in viewDidLoad set tabBar delegate to self and

override func tabBar(_ tabBar: UITabBar, didSelect item:UITabBarItem) 
{
       //MARk: - UITabBarDelegate
if(tabBar.selectedIndex == 0) {
 //Do something
}
else if(tabBar.selectedIndex == 1) {
 //Do something.
} 
}

and if you use tabBarController use this method.And mark delegate as self

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

Very important Note:

If you want to save which tabBar was previously selected you have to save it on your way.Either use flag or NSUserDefaults according to your wish. The reason i mentioned this because i needed to check which tab has been selected right now in View in one of my project.

elk_cloner
  • 2,049
  • 1
  • 12
  • 13
0

Swift 5

Easy way just click on link StakOverFlow screen will open https://stackoverflow.com/a/60539396/6881070

Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34