2015-02-09 116 views
2

我想獲取tabbarController的選定索引。獲取選定的索引tabbar控制器Swift

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

我得到這個錯誤:'UITabBarController?' does not have a member named 'selectedIndex'

我缺少的東西?

回答

8

application.tabBarController是可選的,這意味着它可以是nil。 如果你相信它會永遠nil,這樣做:

var selectedIndex = tabBarController!.selectedIndex 
0

你應該試試這個:

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