Get selected index tabbar controller Swift

与世无争的帅哥 提交于 2019-12-07 14:06:43

问题


I'm trying to get the selected index of the tabbarController.

let application = UIApplication.sharedApplication().delegate as AppDelegate
let tabbarController = application.tabBarController as UITabBarController
let selectedIndex = tabBarController.selectedIndex

I'm getting this error: 'UITabBarController?' does not have a member named 'selectedIndex'

Am I missing something?


回答1:


application.tabBarController is an optional, this means it can be nil. If you are sure it will never be nil, do this:

var selectedIndex = tabBarController!.selectedIndex



回答2:


you should try this:

let application = UIApplication.shared.delegate as! AppDelegate
let tabbarController = application.window?.rootViewController as! UITabBarController
let selectedIndex = tabbarController.selectedIndex


来源:https://stackoverflow.com/questions/28420588/get-selected-index-tabbar-controller-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!