使用按钮在不同选项卡上的视图控制器之间切换(self.tabBarController?.selectedIndex = 1 不起作用)

时间:2020-12-30 01:56:14

标签: swift xcode

我对 XCode 有点陌生,所以我不确定我做错了什么。所以提前感谢任何愿意帮助我的人:)

我在标签栏控制器中嵌入了 2 个以上的视图控制器,每个 vc 都嵌入在导航控制器中,我想使用 vc 中的按钮切换到不同选项卡中的另一个 vc。实际上,我在过去的一个项目中能够做到这一点:我创建了一个 IBAction 函数,该函数在按下按钮时切换到不同的选项卡。下面是我的代码的样子(它完全没有问题)

@IBAction func ButtonPressed(_ sender: UIButton) {

    self.tabBarController?.selectedIndex = 1
}

但是,它不再起作用了,我不知道我是否做了什么不同的事情。有没有解决的办法? (或者为什么这可能不再起作用?)

如果这个描述有点令人困惑,我附上了一张我正在尝试做的截图。

I'm trying to get from the top view controller to the bottom with when the button in the first vc is pressed

1 个答案:

答案 0 :(得分:1)

如果您在导航控制器中,您可能需要执行以下操作: self.navigationController?.tabBarController?.selectedIndex = 1