ios 获取选定的索引标签栏控制器 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28420588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get selected index tabbar controller Swift
提问by Maystro
I'm trying to get the selected index of the tabbarController.
我正在尝试获取 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'
我收到此错误: 'UITabBarController?' does not have a member named 'selectedIndex'
Am I missing something?
我错过了什么吗?
回答by return true
application.tabBarController
is an optional, this means it can be nil
.
If you are sure it will never be nil
, do this:
application.tabBarController
是可选的,这意味着它可以是nil
. 如果您确定它永远不会nil
,请执行以下操作:
var selectedIndex = tabBarController!.selectedIndex
回答by nabu
you should try this:
你应该试试这个:
let application = UIApplication.shared.delegate as! AppDelegate
let tabbarController = application.window?.rootViewController as! UITabBarController
let selectedIndex = tabbarController.selectedIndex