swift中的标签栏项标签问题

时间:2018-01-15 06:42:54

标签: ios tabs

我有一个视图控制器,其中标签栏内有标签栏我有四个标签栏项目。我为每个项目设置了标签。我想在用户点击任何标签栏项目时打开视图控制器。我使用了标签栏的委托方法来实现这一点。现在当我点击任何项目时,它只打开每个标签栏项目上的第一个视图控制器。我的代码就是这个,

 func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if stateBtn.tag == 1 {
        let vcName = "StatesViewController"

        let viewController = storyboard!.instantiateViewController(withIdentifier: vcName)
        self.revealViewController().pushFrontViewController(viewController, animated: true)

    } else if bidBtn.tag == 2 {

        let vcName = "FindBidsViewController"

        let viewController = storyboard!.instantiateViewController(withIdentifier: vcName)
        self.revealViewController().pushFrontViewController(viewController, animated: true)

    }else if categoryBtn.tag == 3{

        let vcName = "CategoriesViewController"

        let viewController = storyboard!.instantiateViewController(withIdentifier: vcName)
        self.revealViewController().pushFrontViewController(viewController, animated: true)

    }else{

        let vcName = "RFPSearchViewController"

        let viewController = storyboard!.instantiateViewController(withIdentifier: vcName)
        self.revealViewController().pushFrontViewController(viewController, animated: true)

    }
}

我已将其委托设置为self.But它只为所有项目打开一个视图控制器。

2 个答案:

答案 0 :(得分:1)

您正在检查

if stateBtn.tag == 1

总是如此,因此第一个条件将始终执行。相反,你应该使用

if item.tag == 1 

因为item是对单击的标签栏项的引用。

答案 1 :(得分:0)

Try this code to detect which item is clicked.

switch item.tag {
     case 0:
        print("First Item")
     case 1:
        print("Second Item")
     case 2:
        print("Third Item")
     case 3:
        print("Fourth Item")
     default: break
}

Hope this would help you.