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.
Swift: How to execute an action when UITabBarItem is pressed
56.7k views Asked by K Swisha AtThere are 6 answers
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                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
    }
}
                        
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                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
didSelectmethod.if let firstVC = viewController as? FirstViewController { firstVC.doSomeAction() }You could do make the
FirstViewControllerimplement the delegate and handle everything there. That way you wouldn't need to make any customUITabBarControllersubclass 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
didSelectmethod above gets called no matter which tab is tapped.UITabBarControllerDelegatedocumentation
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                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")
    }
}
                        
                        
                            
                        
                        
                            On
                            
                            
                                                    
                    
                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 
}
                        

You should use
UITabBarDelegatewith methoddidSelectItem. Use it as any standard delegate:And do not forget to set your tab bar delegate to
selfin view controller.