【问题标题】:Change a view controller's properties based on its tab bar index?根据标签栏索引更改视图控制器的属性?
【发布时间】:2020-04-15 15:24:01
【问题描述】:

我目前在视图控制器上有一个chiclet,当按下它应该导航到位于标签栏中的视图控制器。标签栏中的视图控制器都是自定义视图控制器。当点击chiclet时,我想导航到特定选项卡栏的 selectedIndex 但在切换之前更改该特定选项卡栏的视图控制器上的属性值。例如:

案例.currentMap:

        let selectedIndex = mainTabController.selectedIndex
        let selectedController = mainTabController.selectedViewController
        if selectedController == MapViewController {// error Binary operator '==' cannot be applied to operands of type 'UIViewController?' and 'MapViewController.Type'
            selectedController.isMapSelected = true// Value of type 'UIViewController?' has no member 'isMapSelected'
        }

关于如何执行此属性的任何想法,以便在单击chiclet 后标签栏仍显示在底部?

【问题讨论】:

  • if let selectedController = mainTabController.selectedViewController as? MapViewController 怎么样?

标签: ios swift uitabbarcontroller uitabbar


【解决方案1】:
if let selectedController = mainTabController.selectedViewController as? MapViewController {
    selectedController.isMapSelected = true
}

这将抓取选定的视图控制器,尝试将其转换为MapViewController,如果成功则您可以访问该属性。

【讨论】:

  • 这可行,但我遇到的问题是在切换标签栏控制器的索引时,它在 .isMapSelected 布尔值已更改之前显示 mapController。有没有办法在索引更改之前更改 selectedController 的属性?
  • 如果你知道mapController 的索引,你能在导航到它的索引之前抓住它并更新属性吗? let mapVC = mainTabController.viewControllers[index]
  • 做到了!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-08
  • 2012-07-26
  • 1970-01-01
  • 2021-08-09
  • 2016-12-17
  • 1970-01-01
相关资源
最近更新 更多