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.
6 Answers
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.

- 7,847
- 4
- 37
- 44

- 5,465
- 6
- 39
- 70
-
2Thanks for the answer but how can we set tab bar delegate to self? – Konstantinos Natsios Feb 08 '17 at 23:27
-
3tabBar.delegate = self If it helps you now or someone else in case. – nikhil nangia Nov 01 '17 at 05:05
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
}
}

- 95
- 1
- 14

- 1,301
- 10
- 21
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
}

- 49
- 6

- 4,402
- 37
- 33
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.
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).
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 customUITabBarController
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

- 484,302
- 314
- 1,365
- 1,393
-
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
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")
}
}

- 484,302
- 314
- 1,365
- 1,393
-
So much more elegant, with the required code sitting in the appropriate ViewController. Thank you Suragch. – Mythlandia May 22 '18 at 18:36
-
-
this will break if we push a controller on root controller of any tabbar item and then pop it. since we dont want to execute code at that point – Pranav Gupta Apr 23 '20 at 07:13
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
}
}

- 710
- 4
- 19