获取选定的索引标签栏控制器Swift

May*_*tro 3 selectedindex uitabbarcontroller ios swift

我正在尝试获取tabbarController的选定索引.

let application = UIApplication.sharedApplication().delegate as AppDelegate
let tabbarController = application.tabBarController as UITabBarController
let selectedIndex = tabBarController.selectedIndex
Run Code Online (Sandbox Code Playgroud)

我收到这个错误: 'UITabBarController?' does not have a member named 'selectedIndex'

我错过了什么吗?

ret*_*rue 13

application.tabBarController是可选的,这意味着它可以nil.如果您确定它永远不会nil,请执行以下操作:

var selectedIndex = tabBarController!.selectedIndex
Run Code Online (Sandbox Code Playgroud)