How to Make a Segue to Second Item of Tab Bar

How to make a segue to second item of tab bar?

You can use prepare for "segue" to pass to the tabView the next view.

1) Create a CustomTabViewController and load in your tabView.
Sample Image

2) select the action from the ViewController to the CustomTabBarViewConstroller.
Sample Image

4) Instantiate the buttons in your ViewController and user the sender delegate to select the view to display.

5) Here is the code that you need.

class ViewController: UIViewController {

// Create a value for chosed view
private var nextViewNumber = Int()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "tabBar" {

let nextView = segue.destination as! CustomTabBarViewController

switch (nextViewNumber) {
case 1:
nextView.selectedIndex = 0

case 2:
nextView.selectedIndex = 1

default:
break
}
}
}

@IBAction func FistView(_ sender: UIButton) {
self.nextViewNumber = 1
self.performSegue(withIdentifier: "tabBar", sender: self)
}

@IBAction func SecontView(_ sender: UIButton) {
self.nextViewNumber = 2
self.performSegue(withIdentifier: "tabBar", sender: self)

}

}

Sample Image

How can I segue to the second tab of a tab bar controller from the first 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.

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)
}

How to trigger a segue from a tab bar item to another by clicking a button?

From one of your VCs the tab bar controller, you can access the tab bar controller by accessing parent. If your VC is also embedded in a navigation controller, you need to access parent.parent.

// assuming "self" is embedded in a navigation controller
if let tabBarController = self.parent?.parent as? UITabBarController {

}

After you have the tab bar controller, you can set its selectedIndex to go to whichever tab you want:

tabBarController.selectedIndex = 1 // second tab

From the docs of selectedIndex:

...Setting this property changes the selected view controller to the one at the designated index in the viewControllers array...

How to do a performSegue to a specific view in Tab Bar Controller from another view (swift 4)

with this code, you don't need segues, you can use when you push some button

let VC1 = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! tabBarLoginViewController
VC1.selectedIndex = 2 //this line says that the view that appears will be third of you tab bar controller
self.navigationController!.pushViewController(VC1, animated: true)

if you want to use segue use this, the segue needs to point of tab bar controller, not a view of tab bar controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToMain") {
let vc = segue.destination as! TabBarController
vc.selectedIndex = 2
}
}

How to perform show segue from tab bar controller?

In you NavContToNew , you haven't assigned a delegate.

@IBAction func dismiss(_ sender: Any) {
self.delegate = self.tabBarController as! TabBarCont
//self.dismiss(animated: true, completion: nil)
self.delegate?.sendData(data:self.data)
print("Perform segue delegate")
}

By the way , you don't need to use delegate. You can call your taBarController anywhere in its viewControllers. Here, you can call it in
NavContToNew without using delegate.

self.tabBarController?.performSegue(withIdentifier: "toNew", sender: self.tabBarController)

Embedding 'show' segues in tab bar controller

According to my understanding, whenever you navigate from a controller in the first tab, you would still like to have the ability to select tab 2 from the tab bar.

For this you can make the ViewController in the first tab to be embedded inside a UINavigationViewController. Now whenever you'll push a new controller, your tab bar will still be visible allowing you to switch to other tab.

Hope my understanding of your problem was correct.

Swift navigation controller embedded in tab bar with segue

First you have to select first index of TabbarController and then you have to push PurpleViewController onto the navigation controller like given below

It will solve your both issues

  • You will get Tabbar underneath PurpleViewController
  • secondly If you
    press back button, you will get blueViewController

    tabBarController?.selectedIndex = 0

    if let navC = tabBarController?.viewControllers?.first as? UINavigationController {

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "purpleView") as! PurpleViewController
    navC.pushViewController(nextViewController, animated: true)

    }

Hope it will help you.

How to segue to view controller from TabbarController?

Assuming that your storyboards looks similar to:

Sample Image

That's because you are calling performSegue(with: "identifier") in the TabbarController which does not directly connected to the segue. What you should do instead so to implement it in the first view controller which connected to the navigation controller (ViewControllerA). So, it would be -for instance- something like this:

class ViewControllerA: UIViewController {
// ...

override func viewDidLoad() {
super.viewDidLoad()

performSegue(with: "identifier")

}

// ...
}

Thus your segue should be from ViewControllerA to the desired view controller.



Related Topics



Leave a reply



Submit