Select UITabBarController 中的特定 viewController 不知道其索引
Select a specific viewController in UITabBarController without knowing its index
我有一个由多个选项组成的 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
,但是我想让我的应用程序面向未来,以便如果 viewControllers 在 tabBarController
中的顺序在未来发生变化,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
}
}
我能够自己解决这个问题。下面的代码最适合 my 目标。它相当简洁和优雅(在我看来):
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {
if let browseIndex = (tabBarController?.viewControllers?.filter { [=10=] is UINavigationController} as? [UINavigationController])?.firstIndex(where: { [=10=].viewControllers.first is BrowseViewController }) {
tabBarController?.selectedIndex = browseIndex
}
}
}
注意 BrowseViewController
是 UINavigationController
的第一个 viewController。当然,看到这个答案的用户应该修改他们的代码以适应他们的架构。
我有一个由多个选项组成的 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
,但是我想让我的应用程序面向未来,以便如果 viewControllers 在 tabBarController
中的顺序在未来发生变化,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
}
}
我能够自己解决这个问题。下面的代码最适合 my 目标。它相当简洁和优雅(在我看来):
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {
if let browseIndex = (tabBarController?.viewControllers?.filter { [=10=] is UINavigationController} as? [UINavigationController])?.firstIndex(where: { [=10=].viewControllers.first is BrowseViewController }) {
tabBarController?.selectedIndex = browseIndex
}
}
}
注意 BrowseViewController
是 UINavigationController
的第一个 viewController。当然,看到这个答案的用户应该修改他们的代码以适应他们的架构。