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

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

I found many answers for Objective-C, but not for Swift.

This is the code to execute segue:

if requestsArray.count == 0 { self.performSegueWithIdentifier("offerSegue", sender: self) } else { self.performSegueWithIdentifier("confirm1", sender: self) } 
+6
source share
2 answers

You do not want to cross. Segue creates a new instance of the destination view controller and presents it.

This is why the tab bar disappears. You close your tab bar controller using 2 tabs, with a new instance of your TabBarController2.

You want to switch to another tab.

What you want to do is ask your dashboard owner to switch tabs.

UIViewController has a tabBarController property that allows you to switch to your own tab bar controller.

TabBarControllers have a selectedIndex property that allows you to select one of the tab view controller view controllers to become the active view controller.

So, send a message to your tab bar controller so that it switches to another tab.

EDIT:

Other people besides the OP asked for a code example to illustrate how to do this. I decided to create a sample project illustrating how to do this.

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

I created the base class UIViewController ATabController for the view controllers controlled by the tab bar controller. The ATabController.swift file contains an enumeration indicating which tab you want to select:

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

(Note that the enumeration must be an Objective-C enumeration if you intend to pass parameters of type Tab to IBActions, since IBAction methods must use Objective-C types and function signatures.)

It also includes the TabController protocol:

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

It also defines the delegate tabDelegate :

 weak var tabDelegate: TabController? 

The tab bar controller has a ready-made construct ( prepare(for:sender:) ) that it uses to make itself tabDelegate all view controllers that 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, which are the 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) } 
+16
source

If you are looking for how to switch from one tab to another in the tab controller without using the tab bar, you can do this

 tabBarController?.selectedIndex = [number of tab] 
+13
source

Source: https://habr.com/ru/post/989494/


All Articles