Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get selected index tabbar controller Swift

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?

like image 524
Maystro Avatar asked Feb 09 '15 22:02

Maystro


1 Answers

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
like image 53
return true Avatar answered Oct 13 '22 12:10

return true