Reputation: 5053
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
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