swiftyboi
swiftyboi

Reputation: 3221

How can I segue to the second tab of a tab bar controller from the first tab?

I have no problem actually performing the segue, but when I do, my tab bar disappears from the bottom of the view. I have made a storyboard segue from TabBarController1 to TabBarController2.

I've found a lot of answers for Objective-C, but none for Swift.

This is the code for performing the segue:

if requestsArray.count == 0 {
                self.performSegueWithIdentifier("offerSegue", sender: self)
            } else {
                self.performSegueWithIdentifier("confirm1", sender: self)
            }

Upvotes: 13

Views: 11588

Answers (2)

Duncan C
Duncan C

Reputation: 131418

You don't want to segue. A segue creates a new instance of the destination view controller and presents it.

That's why your tab bar is disappearing. You are covering your tab bar controller, with it's 2 tabs, with a new instance of your TabBarController2.

You want to switch to the other tab.

What you want to do is to ask your owning tab bar controller to switch tabs.

UIViewController has a property tabBarController that lets you get to your owning tab bar controller.

TabBarControllers have a property selectedIndex that let you select one of a tab bar controller's view controllers to become the active view controller.

So, send a message to your tab bar controller asking it to switch to the other tab.

EDIT:

Other people aside from the OP have asked for sample code illustrating how to do this. I decided to create a sample project illustrating how to do it.

You can download it from Github: https://github.com/DuncanMC/TabBarControllers.git

I created a base class of UIViewController ATabController for the view controllers that are managed by the tab bar controller. The ATabController.swift file includes an enum to indicate which tab you want to select:

@objc enum Tab: Int {
  case first = 0
  case second
  case third
}

(Note that the enum has to be an Objective-C enum if you're going to pass parameters of type Tab to IBActions, since IBAction methods need to use Objective-C types and function signatures.)

It also includes a protocol TabController:

@objc protocol TabController {
  @objc func switchTab(to: Tab)
}

It also defines a delegate tabDelegate:

weak var tabDelegate: TabController?

The tab bar controller has a prepareForSegue (prepare(for:sender:)) that it uses to make itself the tabDelegate of all the view controllers it manages as tabs:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let child = segue.destination as? ATabController {
    child.tabDelegate = self
  }

And then it implements the switchTab(to:) method:

@objc func switchTab(to: Tab) {
  let index = to.rawValue
  guard let viewControllerCount = viewControllers?.count,
    index >= 0 && index < viewControllerCount  else { return }
  selectedIndex = index
}

In any of the child view controllers that are tabs of the tab bar controller, you can use IBAction code like this to switch tabs:

@IBAction func handleFirstButton(_ sender: Any) {
  tabDelegate?.switchTab(to: .first)
}

Upvotes: 18

Francisco Escobar
Francisco Escobar

Reputation: 241

If you're looking for how to change from one tab to another in a tab controller without using the tab bar you can do this

tabBarController?.selectedIndex = [number of tab]

Upvotes: 18

Related Questions