Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

segue

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)
            }
like image 740
bkSwifty Avatar asked Jun 22 '15 01:06

bkSwifty


People also ask

How do I segue between view controllers?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.

How do I add more tabs to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


2 Answers

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]
like image 105
Francisco Escobar Avatar answered Oct 22 '22 05:10

Francisco Escobar


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)
}
like image 44
Duncan C Avatar answered Oct 22 '22 06:10

Duncan C