Ios 以编程方式从另一个viewController更改tabBarController索引

Ios 以编程方式从另一个viewController更改tabBarController索引,ios,swift,uiviewcontroller,uitabbarcontroller,uitabbaritem,Ios,Swift,Uiviewcontroller,Uitabbarcontroller,Uitabbaritem,简短的解释 我有一个ContainerViewController,我正在将它推到navigationStack ContainerViewController有两个子UIViewController。一个SlidePanelViewController一个滑出式菜单和一个CenterViewController内容,“我的CenterViewController”是一个UIAbbarController,目前有2个UIViewController 我的菜单中有一个按钮,需要更改tabBarCo

简短的解释

我有一个ContainerViewController,我正在将它推到navigationStack

ContainerViewController有两个子UIViewController。一个SlidePanelViewController一个滑出式菜单和一个CenterViewController内容,“我的CenterViewController”是一个UIAbbarController,目前有2个UIViewController

我的菜单中有一个按钮,需要更改tabBarController中的UIViewController。然而,当我调用我的函数时,什么也没有发生

下面是我试图调用的函数:

func premiumFormulaTapGesture() {
    tabBarController?.selectedIndex = 1
}
我还尝试将其设置为0和2。仍然没有结果。我试着把自己放进去。在它面前没有任何运气

我还尝试将它作为一个函数放在我的ContainerViewController中。但这似乎也不起作用

下面是我如何设置UITabBarController的:

在ViewDidLoad中:

我的tabBarController确实出现了。我可以手动点击它上面的两个按钮,在那里它可以按预期在ViewController之间切换。我稍后计划隐藏UITabBarController,并使用菜单。UITabBarController将成为我从菜单中更改UIViewController的方法

也只是澄清一下。我不使用故事板


如果您能帮助更改我的tabBarController中的ViewController,我们将不胜感激

菲利普·米尔斯还没有给出答案。所以我只是结束这个悬而未决的问题

我的问题是,将名称更改为menuTabBarController后,有两个UITabbarController命名为同一个tabBarController,一切正常


再次感谢Phillip Mills在评论中解决了我的问题。

当您调用tabBarController?时。selectedIndex=1什么是tabBarController?我看到您在viewDidLoad中本地定义了这个变量名,但在PremiumForMulaTap手势中它指的是什么?您完全正确。我制作了一个名为menuTabBarController的新tabBarController,它在类之上定义。并将我的函数更改为menuTabBarController.selectedIndex=1,它工作得非常好。非常感谢你。如果您想继续添加答案,我将接受:
var centerTabBarController: UITabBarController!
let tabBarController = UITabBarController()
let suggestionsVC = mySuggestions_VC()
let testVC = detaiLSuggestion_VC()
let controllers = [suggestionsVC,testVC]
tabBarController.setViewControllers(controllers, animated: false)

centerViewController = tabBarController
view.addSubview(tabBarController.view)
addChildViewController(tabBarController)