简体   繁体   中英

Detecting tap on a tab in UITabBarController

I have created a tabBarController programatically like below

let tabbarController = UITabBarController()
    let homeViewController = HomeViewController()
    let rewardsViewController = RewardsViewController()
    let moreViewController = NewMoreViewController()

    let homeNVc = UINavigationController()
    homeNVc.viewControllers = [homeViewController]

    let rewardsNVc = UINavigationController()
    rewardsNVc.viewControllers = [rewardsViewController]

    let moreNVc = UINavigationController()
    moreNVc.viewControllers = [moreViewController]

    tabbarController.viewControllers = [homeNVc, rewardsNVc, moreNVc]

    tabbarController.tabBar.items![0].title = NSLocalizedString("Dashboard", comment: "")
    tabbarController.tabBar.items![1].title = NSLocalizedString("Prämien", comment: "")
    tabbarController.tabBar.items![2].title = NSLocalizedString("Mehr", comment: "")
    self.window?.rootViewController = tabbarController
}

everyThing is working . I can move through tabs perfectrly, Now I have ta tableView in my homeViewController. Which I want to reload when ever user taps on first tab of my TabBarController. Even if user is already on that viewController I want to reload tableView.

So basically How can I detect that user tapped on first ViewController ?

please guide me thanks :-)

In your homeViewController you may need to implement this delegate method:

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

    //ask where it is first tab bar item 
    if self.tabBarController?.selectedIndex == 0 {
        // your action, e.g.:
        self.tableView.reloadData()
    }

}

NOTE :

You need to have maintained your class like this:

a)

class YourTabBarController: UITabBarController { // inherit from UITabBarController

or this:

b)

class YourViewController: UIViewController, UITabBarDelegate { // set protocol

Invoke UITabBarControllerDelegate and implement this method

func tabBarController(tabBarController: UITabBarController,   didSelectViewController viewController: UIViewController){

}

Just implement the following delegate method,

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

        if item.title == "first tab name"{
            //Do your thing
    }

I have written something similar recently. For consistency, I created one base class BaseTabBarViewController for each Tab I use. But take into account that if a tab was a navigation controller, the one that inherits from the BaseTabBarViewController is the root view controller. This base class implements the UITabBarControllerDelegate protocol. In viewDidLoad, we mark it as delegate. In the delegate method(Objective-c, quite similar in swift 3):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {


if (tabBarController.selected == 0)
{
   // do what you need

To get access to which tabBarItem was tapped override the following function in your custom class for UITabBarController

Swift:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
   guard let index = tabBar.items?.index(of: item) else { return }

   // Do something with the index
}

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