Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITabBarController中选择特定的viewController,而不知道其索引_Ios_Swift_Uitabbarcontroller - Fatal编程技术网

Ios 在UITabBarController中选择特定的viewController,而不知道其索引

Ios 在UITabBarController中选择特定的viewController,而不知道其索引,ios,swift,uitabbarcontroller,Ios,Swift,Uitabbarcontroller,我有一个UItabBarController(称为tabBarController),由许多选项组成。我还有一个UITableView,它的第一行是一个选项,应该让用户导航到特定的viewController 我的didSelectRowAt委托方法如下所示: override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if tableView.cellForRowAt

我有一个UItabBarController(称为
tabBarController
),由许多选项组成。我还有一个UITableView,它的第一行是一个选项,应该让用户导航到特定的viewController

我的
didSelectRowAt
委托方法如下所示:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {
        /* BrowseViewController is currently the second item in the 
           tabBarController, so I just select its index to navigate to it */
        tabBarController?.selectedIndex = 2
    }
}
现在,这适用于我目前的情况,因为我知道
tabBarController
中的第二项是我正在寻找的
UIViewController
,但我想对我的应用程序进行未来验证,以便将来如果
tabBarController
中的viewcontroller顺序发生变化,tableView不会中断

换句话说,我想知道是否有一种方法可以首先从
tabBarController
中提取我要查找的viewController的索引,然后使用该索引导航到它,如下所示:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {

        let browseViewControllerIndex = Int()
        /* iterate through tabBarController's VC's and if the type of the VC 
       is BrowseViewController, find its index and store it
       in browseViewController */
    }
 }

您可以尝试以下方法:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {

        // safely get the different viewcontrollers of your tab bar
        // (viewcontrollers is an optional value)
        guard let tabs = tabBarController.viewcontrollers else { return }

        // index(of:) gets you the index of the specified class type.
        // Also an optional value
        guard let index = tabs.index(of: BrowseViewController()) else { return }
        tabBarController?.selectedIndex = index
    }
}

我能自己解决这个问题。下面的代码最适合我的目标。它相当简洁和优雅(在我看来):


请注意,
BrowseViewController
UINavigationController
的第一个视图控制器。当然,看到这个答案的用户应该修改他们的代码以适应他们的体系结构。

基本上,您可以创建
UITabBarController
的子类,并使用方便的方法在需要的时间访问所需的控制器。通过设计,您已经拥有了所需的一切:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {

        if let browseIndex = (tabBarController?.viewControllers?.filter { $0 is UINavigationController} as? [UINavigationController])?.firstIndex(where: { $0.viewControllers.first is BrowseViewController }) {
            tabBarController?.selectedIndex = browseIndex
        }

    }

}