Alex Priest Alex Priest - 2 years ago 336
Swift Question

Programmatically switch view controller to show views in Tab Bar Controller

I am trying to figure out how I can programmatically switch the views from one view controller to a first view controller in a Tab bar controller when I press a button.

Currently I have a view controller with three buttons. When I press a button I would like to then switch. This is the following code I have for this screen. It is called the second view controller.

import UIKit

class SecondViewController: UIViewController {

//Button Outlets
@IBOutlet var ButtonEndOfShift: UIButton!
@IBOutlet var ButtonMultiTimes: UIButton!
@IBOutlet var ButtonEndAndLunch: UIButton!

override func viewDidLoad() {

// Do any additional setup after loading the view, typically from a nib.

//hides tab bar controller
self.tabBarController?.tabBar.hidden = true

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

//Changes screen for End Of Shift Button
@IBAction func ChangeNextViewLow(sender: AnyObject) {

self.performSegueWithIdentifier("segue", sender: nil)

//Changes screen for Lunch and End Of Shift Button
@IBAction func ChangeNextViewMedium(sender: UIButton) {

//Changes screen for Multiple Times button
@IBAction func ChangeNextViewHigh(sender: UIButton) {


enter image description here

Answer Source

I have added UITabBarController in Storyboard like below please see images.

enter image description here enter image description here

Then i have written following functions for your help.

// For navigate to Tabbar Controller
    @IBAction func btnClick () {
        self.performSegueWithIdentifier("GoToTabBar", sender: nil)

    // For switching between tabs
    func switchTab (index : Int) {
       self.tabbarController.selectedIndex = index

You can also set UITabBarController as your application RootViewController.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download