26

Currently I have a Tab Bar Controller that is connected to a tableview controller. I'm trying to go to the top of the tableview when I press the tab bar item. I know how to get to the top of the tableview. I just don't know how to do an action when the item is pressed.

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
K Swisha
  • 283
  • 1
  • 3
  • 6

6 Answers6

34

You should use UITabBarDelegate with method didSelectItem. Use it as any standard delegate:

class yourclass: UIViewController, UITabBarDelegate {
    func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
        //This method will be called when user changes tab.
    }
}

And do not forget to set your tab bar delegate to self in view controller.

Hans Brende
  • 6,057
  • 4
  • 27
  • 40
Nikita Zernov
  • 5,146
  • 6
  • 35
  • 67
20

Here is an answer to this question

Basically you do this:

  • Make sure your view controller is subscribed to the UITabBarDelegate
  • Set tags in IB for each tab bar item
  • Implement the didSelectItem method, something like this:

    -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
        if(item.tag == 1) {
           // Code for item 1
        }
        else if(item.tag == 2) {
           // Code for item 2
        }
    }
    

This will give you access to each tab item tapped event. Hope it helps!

In Swift:

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(item.tag == 1) {
        // Code for item 1
    } else if(item.tag == 2) {
        // Code for item 2
    }
}
erparker
  • 1,301
  • 10
  • 20
12

SWIFT 3

class yourclass: UIViewController, UITabBarDelegate {
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print("Test")
    }
}

And do not forget to set your tabBar delegate to self in viewDidLoad

override func viewDidLoad(){
    super.viewDidLoad()
    <YOUR TAB BAR NAME>.delegate = self 
}
ushehri
  • 37
  • 5
Ahmed Safadi
  • 3,623
  • 30
  • 28
11

I was having trouble implementing the other answers here. This is a fuller answer. It assumes you are using a UITabBarController (the default if you create a new Tabbed App). This solution will print a message every time a view controller tab button is tapped.

enter image description here

Code

Create a new Swift file called MyTabBarController.swift. Paste in the following code.

import UIKit

class MyTabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // tell our UITabBarController subclass to handle its own delegate methods
        self.delegate = self
    }

    // called whenever a tab button is tapped
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {

        if viewController is FirstViewController {
            print("First tab")
        } else if viewController is SecondViewController {
            print("Second tab")
        }
    }
}

Interface Builder

On your storyboard select the Tab Bar Controller. Then in the Identity inspector, set the class name to MyTabBarController (that is, the name of the class in the code above).

enter image description here

That's all. You can run your app now and be notified whenever the user taps a tab bar item.

Notes

  • If you need to run a method on a tap, then you can do something like the following in didSelect method.

    if let firstVC = viewController as? FirstViewController {
        firstVC.doSomeAction()
    }
    
  • You could do make the FirstViewController implement the delegate and handle everything there. That way you wouldn't need to make any custom UITabBarController subclass and set it in IB. However, having a child do the parent's work seems like the wrong place to do it. Anyway, here is is:

    class FirstViewController: UIViewController, UITabBarControllerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tabBarController?.delegate = self
        }
    
        func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
            // ...
        }
    }
    

    The didSelect method above gets called no matter which tab is tapped.

  • UITabBarControllerDelegate documentation

Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
  • Thanks for `tabBarController` method. The accepted answer was using `tabBar` method instead, and seems to work on ViewController, but not on TabBarController – Isaac Bosca May 25 '18 at 11:06
5

An alternate solution is to just do something in viewDidAppear in whichever View Controller the tab shows.

First Tab View Controller

import UIKit
class FirstViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("First tab")
    }
}

Second Tab View Controller

import UIKit
class SecondViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("Second tab")
    }
}
Suragch
  • 364,799
  • 232
  • 1,155
  • 1,198
0
class TestViewController: UIViewController,UITabBarDelegate {

    @IBOutlet weak var tabbar: UITabBar!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tabbar.delegate = self
    }
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        print(tabBar.items![1]) // The number is tab index
    }
}
Ryan110
  • 638
  • 4
  • 18