在 Swift 中以编程方式切换标签栏
Switch tab bar programmatically in Swift
我有一个标签栏应用程序,我的第一个视图上有一个按钮,我想在按下时以编程方式在标签栏中切换到我的第二个标签。
我似乎不太明白如何让索引等切换到它我试过这样的东西。
tababarController.selectedIndex = 1
没有成功。
这很简单 tabBarController 被声明为可选类型
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.
所以你只需要加上“?”最后:
@IBAction func goToSecond(_ sender: Any) {
tabBarController?.selectedIndex = 1
}
Leo Dabus 提供的解决方案(见上文)对我来说效果很好。但是 - 某些控件状态不佳。无法解决这个问题,但这个小解决方法对你们都有好处:
func switchToDataTab(){
NSTimer.scheduledTimerWithTimeInterval(0.2,
target: self,
selector: "switchToDataTabCont",
userInfo: nil,
repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
添加到安东尼的代码:
func switchToDataTab(){
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil,repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
其中选择器 class 已更改为
#selector(switchToDataTabCont)
Swift 3:
func switchToDataTab() {
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil, repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
Swift 4+:
func switchToDataTab() {
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil, repeats: false)
}
@objc func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
我有一个标签栏应用程序,我的第一个视图上有一个按钮,我想在按下时以编程方式在标签栏中切换到我的第二个标签。
我似乎不太明白如何让索引等切换到它我试过这样的东西。
tababarController.selectedIndex = 1
没有成功。
这很简单 tabBarController 被声明为可选类型
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.
所以你只需要加上“?”最后:
@IBAction func goToSecond(_ sender: Any) {
tabBarController?.selectedIndex = 1
}
Leo Dabus 提供的解决方案(见上文)对我来说效果很好。但是 - 某些控件状态不佳。无法解决这个问题,但这个小解决方法对你们都有好处:
func switchToDataTab(){
NSTimer.scheduledTimerWithTimeInterval(0.2,
target: self,
selector: "switchToDataTabCont",
userInfo: nil,
repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
添加到安东尼的代码:
func switchToDataTab(){
NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil,repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
其中选择器 class 已更改为
#selector(switchToDataTabCont)
Swift 3:
func switchToDataTab() {
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil, repeats: false)
}
func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}
Swift 4+:
func switchToDataTab() {
Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(switchToDataTabCont), userInfo: nil, repeats: false)
}
@objc func switchToDataTabCont(){
tabBarController!.selectedIndex = 0
}