簡體   English   中英

如何在標簽欄控制器中快速隱藏視圖控制器?

[英]how hide a view controller in tab bar controller swift?

如何在標簽欄中隱藏某些視圖控制器?

例如,我的標簽欄有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”具有按鈕,圖像和文本。

我認為這非常容易。首先,您需要添加所有標簽欄項,然后僅根據您的用戶角色隱藏標簽欄索引,如下所示。

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
    }
  }
}

根據您的用戶類型設置視圖控制器是正確的,因為您必須從情節提要中初始化UIViewController,所以具有黑色視圖:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM