根据标签栏索引更改视图控制器的属性?

Change a view controller's properties based on its tab bar index?

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

案例.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 {
    selectedController.isMapSelected = true
}

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