【问题标题】:Tab bar item tag issue in swiftswift中的标签栏项目标签问题
【发布时间】:2018-01-15 06:42:54
【问题描述】:

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

 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。但它只为所有项目打开一个视图控制器。

【问题讨论】:

    标签: ios tabs


    【解决方案1】:

    你正在检查

    if stateBtn.tag == 1
    

    这始终为真,因此第一个条件将始终执行。相反,您应该使用

    if item.tag == 1 
    

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

    【讨论】:

    • 那么第一次设置什么? @sumesh shivan
    【解决方案2】:
    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2018-11-16
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      相关资源
      最近更新 更多