Ios 如何在swift中更改初始选项卡栏选择?

Ios 如何在swift中更改初始选项卡栏选择?,ios,swift,cocoa-touch,Ios,Swift,Cocoa Touch,在试图找到解决方案时,我遇到了以下答案: [] 它似乎完成了我想要的,但在目标C中: UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController; tabBar.selectedIndex = 0; 我如何在swift中执行此操作?我的第一次尝试返回一个错误: 无法将表达式的类型“$T4???”转换为类型“UITabBarController” 为我工作!您只需将添加为!UITABBARC控

在试图找到解决方案时,我遇到了以下答案:

[]

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

UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
tabBar.selectedIndex = 0;
我如何在swift中执行此操作?我的第一次尝试返回一个错误:

无法将表达式的类型“$T4???”转换为类型“UITabBarController”


为我工作!您只需将
添加为!UITABBARC控制器

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

为我工作!您只需将
添加为!UITABBARC控制器

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

您确定您的rootViewController是UITabBarController吗?rootViewController是否与初始视图控制器相同?如果是这样,那么yesIt应该是。我会启动一个游乐场,看看是否可以复制这个问题。你确定你的rootViewController是uitabarcontroller吗?rootViewController和初始视图控制器是一样的吗?如果是这样,那么yesIt应该是。我会启动一个游乐场,看看是否可以复制这个问题