简体   繁体   中英

Detect Tab bar item change for UITabBarController

Is there a way, such as a delegate, for the tab bar controller to do something when you tap an item?

The action should only be performed once when the view is displayed and not if you repeatedly press on the tab button.

I am using this delegate, but it does not work well since every time I touch on the tab it starts (instead of starting only at each tab change)

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    print("Hey")
}

You can try to implement shouldSelect beside didSelect

func tabBarController(_ tabBarController: UITabBarController, 
              shouldSelect viewController: UIViewController) -> Bool {

    return tabBarController.viewControllers.index(of:viewController) != tabBarController.selectedIndex
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM