【问题标题】:how hide a view controller in tab bar controller swift?如何在标签栏控制器中快速隐藏视图控制器?
【发布时间】:2018-08-04 14:13:22
【问题描述】:

如何在标签栏中隐藏一些视图控制器?

例如,我的标签栏有 3 个视图控制器,但我的应用程序有两种类型的用户:

- user 1 can only access one of those view controllers
- user 2 can access the 3 view controllers

如何隐藏某些视图控制器,知道用户是谁?

我有这个代码

if user1 == 'admin'
  {
    let tabone = EstadisticasViewController()
    let tab1 = UITabBarItem(title: "Estadisticas", image: nil, selectedImage: nil)

    tabone.tabBarItem = tab1
    self.viewControllers = [tabone]
  }

但结果我有一个黑色的视图,在我的故事板视图中“estadísticas”有按钮、图像和文本。

【问题讨论】:

    标签: ios swift view controller tabs


    【解决方案1】:

    我认为这很容易 首先,您需要添加所有标签栏项目, 然后只需像这样根据您的用户角色隐藏标签栏索引..

    if user1 == 'admin'
      {
      //Show All Tabs
    }
    else
    {
      let indexToRemove = 3
       if let tabBarController = self.tabBarController {
    
        if indexToRemove < tabBarController.viewControllers?.count {
            var viewControllers = tabBarController.viewControllers
            viewControllers?.remove(at: indexToRemove)
            tabBarController.viewControllers = viewControllers
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      根据你的用户类型设置你的视图控制器是正确的,你有一个黑色的视图,因为你必须从情节提要中初始化你的 UIViewController:

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      let tabOneVC = storyboard.instantiateViewController(withIdentifier: "Your identifier") as! EstadisticasViewController
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 2015-02-13
        相关资源
        最近更新 更多