ios - 如果选项卡栏 Controller 在另一个 ui Controller 内,如何以编程方式切换选项卡? - swift

标签 ios swift uitabbarcontroller

嗨,我是 ios 开发的新手,尤其是 swift。基本上我只需要切换标签,我试过 tabBarController?.selectedIndex = 1 但它似乎不能切换标签。

enter image description here

上图是我的 Storyboard,初始设置是我有 splitview Controller ,master 上的初始 View 是 tableViewController,它是 A当点击单元格的附件时,Detail of A 的细节出现在 splitview 的细节上,当点击行本身Tab Bar Controller 替换 master

然后我的 tabBarController 中有四个选项卡 Tab BTab CTab DTab E 当点击它时它只是一个列表,它显示其各自的 View Controller 作为 splitview 的详细信息然后在我的 tab E 我有 3 个按钮,每个按钮设计用于切换选项卡

注意:选项卡和详细信息嵌入在导航 Controller 中

我对如何以及标准是什么感到有点困惑。 谁能帮我提出任何意见,建议就可以了。

最佳答案

原因

 tabBarController?.selectedIndex = 1 

不工作,tabBarController 是 nil。

为了获得正确的 tabBarController,您必须从 window 对象的根目录中获取它。 像这样,

var tabBarController: UITabBarController = self.window?.rootViewController as? UITabBarController
tabBarController.selectedIndex = 1

因为,您可能必须在导航堆栈中找到正确的 tabBarController,因此您也必须遍历堆栈。 从上面的 Storyboard 来看,我相信您正在从 Tableview didSelect 加载 tabbarController,因此您必须在那里获取 tabBarController 并在那里选择 tabbar index。

希望对您有所帮助。

关于ios - 如果选项卡栏 Controller 在另一个 ui Controller 内,如何以编程方式切换选项卡? - swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30206263/

相关文章:

swift - currentContext 和 overCurrentContext 有什么区别?

ios - 在 Swift 中,如果我有一个 UITabBarController,我如何在切换选项卡之前弹出我当前的 navigationController?

iphone - 如何在UITabBarController中的两个UIViewController之间传递信息

iOS:用户在 TextView 中键入后无法最小化键盘(xcode)

iOS如何知道坐标是否正确

cocoa-touch - 间接使用 NSManagedObject 时发送到已释放实例的消息

swift - 如何重新渲染 MKMapView 以根据新坐标更新覆盖

ios - UISlider 每毫秒都不会顺畅地滴答作响

ios - Xcode 12 启动模拟器非常慢

objective-c - 在选项卡栏和导航栏内弹出 View 时遇到问题