代码之家  ›  专栏  ›  技术社区  ›  TheNeil

如何在UITabBarController中获取特定UIViewController的索引

  •  0
  • TheNeil  · 技术社区  · 6 年前

    所以,从一个标签程序切换 UITabBarController 另一个很容易。。。

    self.tabBarController?.selectedIndex = 1

    有没有一种方法可以检索某个特定项目的[第一个]索引 UIViewController

    if let destinationIndex = self.tabBarController?.index(of: ExampleViewController) {
        self.tabBarController?.selectedIndex = destinationIndex
    }
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Craig Siemens    6 年前

    你会想用 firstIndex(where:) 方法。

    if let destinationIndex = self.tabBarController?.viewControllers.firstIndex(where: { $0 is ExampleViewController }) {
        self.tabBarController?.selectedIndex = destinationIndex
    }
    
        2
  •  0
  •   TheNeil    6 年前

    Craig Siemens ,适用于目标VC嵌入导航控制器时:

    if let destinationIndex = self.tabBarController?.viewControllers?.firstIndex(where: {
        let navController = $0 as? UINavigationController
        return navController?.viewControllers.first is ExampleViewController
    }) {
        self.tabBarController?.selectedIndex = destinationIndex
    }