[ACCEPTED]-How to get the 'current' navigation controller from tab bar controller-uitabbarcontroller

Accepted answer
Score: 69

Use the UITabBarControllers selectedViewController property.

navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];

0

Score: 4

I think UITabBarController selectedViewController property should be what you are 2 looking for.

So, from a UITabBarController 1 method :-

 [self.selectedViewController pushViewController:someViewController animated:YES];
Score: 4

Updating Starsky's Swift answer to iOS 13 2 ("'keyWindow' was deprecated in iOS 1 13.0")

guard let tabBarVC = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController as? UITabBarController else { return }
    if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
...
}
Score: 2

A Swift version, in case someone can't read 4 Objective-C, with an additional solution 3 for how to find the tabBar from anywhere. I 2 use this to push to a screen when processing 1 a notification.

guard let tabBarVC = UIApplication.shared.keyWindow?.rootViewController as? UITabBarController else { return }

if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
    currentNavController.pushViewController(someVC, animated: true)
}

More Related questions