VitDuck
VitDuck

Reputation: 93

How to get name of previous ViewController when switching TabBar Item

In case I have 4 items in Tabar.

When open app, default is in TabBarItem[0] - ViewControllerRoot I push from ViewControllerRoot into ViewController A. From ViewController A, I switch to TabBarItem[3] - contain ViewController B.

The question is: How can I get name of ViewController A when I switch to TabBarItem[3]?

Thank you so much.

Upvotes: 1

Views: 915

Answers (3)

Prashant
Prashant

Reputation: 336

Your question is a bit unclear..please share some code.

  • if you wish to get an the instance of ViewController A: window.rootViewController?.childViewControllers[0]
  • if you wish to get the class name of ViewController A:

a = window.rootViewController?.childViewControllers[0] var className: String = a.self

Upvotes: 0

Maulik Kundaliya
Maulik Kundaliya

Reputation: 462

You can get your current viewcontroller using selectedViewController.

User delegate method of tabbar.

    func tabBarController(_ tabBarController :UITabBarController, shouldSelect: UIViewController){
let currentVC = tabBarController.selectedViewController
let destinationVC = shouldSelect
}

I hope this will work for you.

Upvotes: 4

coding-flash
coding-flash

Reputation: 23

When you change the view from UITabBar there is a delegate method that is invoked

tabBarController:animationControllerForTransitionFromViewController:toViewController:

This will help you to find the previous selected controller.

Upvotes: 0

Related Questions