如何更改swift中的初始标签栏选择?

时间:2015-05-15 08:43:24

标签: ios swift cocoa-touch

在尝试寻找解决方案时,我遇到了这个答案:

[https://stackoverflow.com/a/10441200/4856759]

它似乎完成了我想要的,但在目标C:

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;

我如何在swift中执行此操作?我的第一次尝试返回错误:

  

无法转换表达式的类型'$ T4 ??'输入'UITabBarController'

var tabBar: UITabBarController = self.window?.rootViewController
tabBar.selectedIndex = 1

1 个答案:

答案 0 :(得分:3)

为我工作!您只需添加as! UITabBarController

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