简体   繁体   English

检测UITabBarController中选项卡上的点击

[英]Detecting tap on a tab in UITabBarController

I have created a tabBarController programatically like below 我已经以编程方式创建了一个tabBarController,如下所示

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 . everyThing正在运作。 I can move through tabs perfectrly, Now I have ta tableView in my homeViewController. 我可以完全切换标签,现在我在homeViewController中有ta tableView。 Which I want to reload when ever user taps on first tab of my TabBarController. 当用户点击我的TabBarController的第一个标签时,我想重新加载。 Even if user is already on that viewController I want to reload tableView. 即使用户已经在viewController上,我想重新加载tableView。

So basically How can I detect that user tapped on first ViewController ? 那么基本上如何检测用户点击第一个ViewController?

please guide me thanks :-) 请指导我谢谢:-)

In your homeViewController you may need to implement this delegate method: homeViewController中,您可能需要实现此委托方法:

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) b)

class YourViewController: UIViewController, UITabBarDelegate { // set protocol

Invoke UITabBarControllerDelegate and implement this method 调用UITabBarControllerDelegate并实现此方法

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. 为了保持一致性,我为每个使用的Tab创建了一个基类BaseTabBarViewController。 But take into account that if a tab was a navigation controller, the one that inherits from the BaseTabBarViewController is the root view controller. 但是考虑到如果选项卡是导航控制器,则继承自BaseTabBarViewController的选项卡是根视图控制器。 This base class implements the UITabBarControllerDelegate protocol. 此基类实现UITabBarControllerDelegate协议。 In viewDidLoad, we mark it as delegate. 在viewDidLoad中,我们将其标记为委托。 In the delegate method(Objective-c, quite similar in swift 3): 在委托方法中(Objective-c,在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 要访问哪个tabBarItem,请在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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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