ios - 如何在 UITabBarController 中获取特定 UIViewController 的索引
问题描述
因此,以编程方式从一个选项卡切换UITabBarController
到另一个选项卡很容易......
self.tabBarController?.selectedIndex = 1
...但是每次对标签索引进行硬编码似乎很不优雅。例如,如果重新排序选项卡,则会导致问题。
有没有办法检索特定的 [first] 索引UIViewController
,以便代码可能更像以下内容?
if let destinationIndex = self.tabBarController?.index(of: ExampleViewController) {
self.tabBarController?.selectedIndex = destinationIndex
}
解决方案
您将要使用该firstIndex(where:)
方法。
if let destinationIndex = self.tabBarController?.viewControllers.firstIndex(where: { $0 is ExampleViewController }) {
self.tabBarController?.selectedIndex = destinationIndex
}
推荐阅读
- php - Symfony 3 - 注意:反序列化():28 字节偏移量 11 处出错
- asp.net-core-mvc - 在 MVC Core 中使用现有数据库时出现 InvalidOperationException
- azure - HTTP 502:当一个函数与另一个计划函数保持温暖时
- java - 如何从蓝图 xml 文件中获取路线
- django - Django FileField() 禁用上传
- git - 无法提交新文件
- apache - 重定向到带有标签的 url .htaccess Apache
- jquery - 语言下拉菜单可点击?
- r - 根据data.table中列的最大值自定义dcast内部的聚合函数?
- keras - 在 keras 中使用 2Gb gpu 推断大图像