Enamul Haque
Enamul Haque

Reputation: 5053

UITabBar item did select is not calling in swift 5

I have used tabbar in swift 5 . But when i click in tabbar item, tabbar didselect item not calling.But I did not used TabbarViewController. I have used bellow code in my viewController..

class ViewController: UIViewController,UITabBarDelegate {

 @IBOutlet weak var bottomTab: UITabBar!

  override func viewDidLoad() {
     super.viewDidLoad()
     bottomTab.delegate = self
  }

 func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
      print("Selected item")
   }

 }

Please help me to calling didSelectItem from viewController

Upvotes: 0

Views: 1203

Answers (2)

Ammar
Ammar

Reputation: 380

1) In your ViewController inherit UITabBarControllerDelegate

2) Set delegate in a viewDidLoad

3) Add a function

Example:

class ViewController: UIViewController, UITabBarControllerDelegate {

   func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let tabBarIndex = tabBarController.selectedIndex
        if tabBarIndex == 0 {
            //do your stuff
        }
   }

   override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.delegate = self
   }

}

Upvotes: 1

rs7
rs7

Reputation: 1630

I believe you are implementing the incorrect method. Try implementing this method instead:

func tabBar(UITabBar, didSelect: UITabBarItem) Sent to the delegate when the user selects a tab bar item.

Source

Upvotes: 0

Related Questions