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)
}
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.
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.
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]
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With