How to get the ViewController from UITabBarItem

When a user taps on an item, I want to get the ViewController associated with that tab.

The TabBar delegate no longer provides the ViewController delegate. Instead, it provides a didSelectItem delegate.

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

How do I get the ViewController from UITabBarItem?


2 Answers How to get the ViewController from UITabBarItem

If you're using a UITabBarController and not just a UITabBar look into using UITabBarControllerDelegate rather than UITabBarDelegate. UITabBarControllerDelegate provides the method:

func tabBarController(_ tabBarController: UITabBarController, 
    didSelectViewController viewController: UIViewController)

3 year ago

This is a swift 4 Extension version that works for me:

import UIKit

extension UIApplication {

var visibleViewController: UIViewController? {

    guard let rootViewController = keyWindow?.rootViewController else {
        return nil

    return getVisibleViewController(rootViewController)

private func getVisibleViewController(_ rootViewController: UIViewController) -> UIViewController? {

    if let presentedViewController = rootViewController.presentedViewController {
        return getVisibleViewController(presentedViewController)

    if let navigationController = rootViewController as? UINavigationController {
        return navigationController.visibleViewController

    if let tabBarController = rootViewController as? UITabBarController {
        // Uncomment the line bellow the TabBarController
        //return tabBarController.selectedViewController

        // uncomment the line bellow to get the visible ViewController of the TabBarController
        return getVisibleViewController(tabBarController.selectedViewController!)

    return rootViewController

This can be called as easy as this:

let visibleVC = UIApplication.shared.visibleViewController

I hope this work for you as well ;)

6 days ago