bbabbitt
bbabbitt

Reputation: 3

How can I detect programmatically (e.g.IBAction) when the tab selection changes views?

I am using Swift 5. I would like to be able to programmatically detect when a tab bar item is clicked to select a different ViewController. The ideal situation would be to be able to set an IBAction event for the tab item selection. While I can establish an outlet for the tab bar item, I can't seem to find a way to set an IBAction for the item. Is there a way to do this?

Upvotes: -4

Views: 211

Answers (1)

Masoud Roosta
Masoud Roosta

Reputation: 475

add UITabBarControllerDelegate to your tabBarViewContriller then use this function.

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

    print("last selected index = \(lastSelectedIndex)") //show last index
    print("selected index = \(selectedIndex)") //show current selection index
}

Upvotes: 0

Related Questions