在Swift中更改UITabBar selectedItem [英] Change UITabBar selectedItem in Swift
本文介绍了在Swift中更改UITabBar selectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式更改UITabBar中的所选项目?
How can I programmatically change the selected item in a UITabBar?
推荐答案
Swift 3及更高版本
从Swift 3开始,您还可以使用
Swift 3 and later
As of Swift 3, you can also use
tabBarController.selectedIndex = 0 // (or any other existing index)
(谢谢你,@ nidomiro。)
(Thank you, @nidomiro.)
请尝试以下
tabBar.selectedItem = tabBar.items![newIndex] as! UITabBarItem
假设您有权访问 UITabBarController
拥有 UITabBar
,您可以执行以下操作
Assuming you have access to the UITabBarController
that owns the UITabBar
, you can do the following
self.selectedViewController = self.viewControllers![newIndex] as! UIViewController
上面的代码行应放在 UITabBarController
子类。
The above line of code should be put somewhere inside of the UITabBarController
subclass.
但如果您可以从外部访问标签栏控制器,请执行以下操作
But if you have access to the tab bar controller from "outside," do the following
tabBarController.selectedViewController = tabBarController.viewControllers![newIndex] as! UIViewController
这篇关于在Swift中更改UITabBar selectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文