How to get the 'current' navigation controller from tab bar controller
Updating Starsky's Swift answer to iOS 13 ("'keyWindow' was deprecated in iOS 13.0")
guard let tabBarVC = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController as? UITabBarController else { return }
if let currentNavController = tabBarVC.selectedViewController as? UINavigationController {
...
}
Use the UITabBarController
s selectedViewController property.
navcon = (UINavigationController*)myTabBarController.selectedViewController;
[navcon pushViewController:someViewController animated:YES];
A Swift version, in case someone can't read Objective-C, with an additional solution for how to find the tabBar from anywhere. I use this to push to a screen when processing 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)
}
I think UITabBarController selectedViewController
property should be what you are looking for.
So, from a UITabBarController method :-
[self.selectedViewController pushViewController:someViewController animated:YES];