当iOS应用程序启动时,我需要编写一些代码将视图切换到另一个选项卡(例如,默认情况下显示的是第二个选项卡,而不是第一个选项卡)。
我是Swift的新手,我做了以下工作:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions")
self.showViewController(vc as UIViewController, sender: vc)我认为答案可能在于使用UITabbarController.selectedIndex = 1,但不太确定如何实现这一点。
发布于 2014-08-15 12:10:32
如果您的window rootViewController是UITabbarController(在大多数情况下都是UITabbarController),那么您可以在AppDelegate文件中访问didFinishLaunchingWithOptions中的tabbar。
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}这将使用index (1)在selectedIndex中打开选项卡。
如果您在viewDidLoad of您的firstViewController中这样做,您需要通过标志来管理,或者用另一种方法来跟踪所选的选项卡。在您的didFinishLaunchingWithOptions文件或rootViewController自定义类viewDidLoad中执行此操作的最佳位置。
发布于 2016-09-27 03:17:17
Swift 3
可以将此代码添加到index 0中的默认视图控制器(TabBarController)中:
override func viewWillAppear(_ animated: Bool) {
_ = self.tabBarController?.selectedIndex = 1
}加载后,这将自动将选项卡移动到列表中的第二项,但也允许用户在任何时候手动返回该视图。
发布于 2015-07-10 07:37:42
1.创建一个超UITabBarController的新类。例如:
class xxx: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}2.向函数viewDidLoad()添加以下代码:
self.selectedIndex = 1; //set the tab index you want to show here, start from 03.转到故事板,并将Tab Bar控制器的自定义类设置为这个新类。(图中以MyVotes1为例)

https://stackoverflow.com/questions/25325923
复制相似问题