UITabBarController and UINavigationController Interaction
I have an app with a tab bar with two tabs. One tab displays a UINavigationController, the other a UIViewController that I have customized.
What I have noticed is that if switch tabs to the UINavigationController tab then navigate a few levels deep in UITableViews, if I click on the current tab on the UITabBar the UINavigationController pops to it's root view.
I was wondering how this takes place. It does not appear that the UINavigationController is a delegate of the UITabBar or UITabBarController, which would have been one option. The functionality is somehow automatic.
I want to implement a similar action on my UIView in the seco开发者_如何学Pythonnd tab, so I'd like to figure this out. Thanks!
What UITabBarController
is doing is that whenever you tap a already selected tab, it checks if the UIViewController
of that tab is an UINavigationController
. If it is, then pop to the rootViewController.
What you want to do is to set you second tab the delegate of your UITabBarController
and check for
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
When ever that method is fired, check if the viewController
is your second tab viewController and that the selectedIndex
(of UITabBarController
) is 1. If that's the case, implement your action.
This works only on iOS 3.0 or later. In versions of iOS prior to version 3.0, this method is called only when the selected view controller actually changes.
This is actually a great question, I never noticed this behavior until today.
I had a quick guess about this and after some testing, I seem to be correct: Since your TabBarController knows, that it's tab contains a UINavigationController, it simply calls the popToRootViewControllerAnimated:
method of the NavigationController. I tested this by creating a category, which overwrites the popToRootViewControllerAnimated:
method (which, of course, you shouldn't do in your app) and voila, this method is in fact called.
This answers your question about the "how". If a can think about a way to reproduce this in your ViewController, I'll let you know.
精彩评论