Azabella Azabella - 1 year ago 85
Swift Question

Switch tab bar programatically in Swift

I have a tab bar application and i have a button on my first view which i want to when pressed switch to my second tab programatically in the tab bar.

I can't quite seem to figure it out how to get the index etc to switch to it i've tried stuff like this.

tababarController.selectedIndex = 1

With no success.

Answer Source

Thats pretty simple (just make sure your Tab Bar Controller Identity Storyboard ID is set to "tabBarController" and you need to add "?" at the end of it:

@IBAction func goToSecond(sender: AnyObject) {
    tabBarController?.selectedIndex = 1