在 Swift 中以编程方式切换标签栏 [英] Switch tab bar programmatically in Swift
问题描述
我有一个标签栏应用程序,我的第一个视图上有一个按钮,我想在按下时以编程方式切换到标签栏中的第二个标签.
我似乎不太明白如何让索引等切换到它我已经尝试过这样的东西.
tababarController.selectedIndex = 1
没有成功.
这很简单,tabBarController 被声明为可选类型
var tabBarController: UITabBarController?{ 得到 }
<块引用>
视图控制器层次结构中最近的祖先是标签栏控制器.如果视图控制器或其祖先之一是孩子标签栏控制器,此属性包含拥有的标签栏控制器.如果视图控制器不是,则此属性为零嵌入在标签栏控制器中.
所以你只需要添加?"最后:
@IBAction func goToSecond(_ sender: Any) {tabBarController?.selectedIndex = 1}
I have a tab bar application and i have a button on my first view which i want to when pressed switch to my second tab programmatically in the tab bar.
I can't quite seem to figure it out how to get the index etc to switch to it i've tried stuff like this.
tababarController.selectedIndex = 1
With no success.
Thats pretty simple tabBarController is declared as an optional type
var tabBarController: UITabBarController? { get }
The nearest ancestor in the view controller hierarchy that is a tab bar controller. If the view controller or one of its ancestors is a child of a tab bar controller, this property contains the owning tab bar controller. This property is nil if the view controller is not embedded inside a tab bar controller.
So you just need to add "?" at the end of it:
@IBAction func goToSecond(_ sender: Any) {
tabBarController?.selectedIndex = 1
}
这篇关于在 Swift 中以编程方式切换标签栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!