views:

73

answers:

2

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 second tab, so I'd like to figure this out. Thanks!

+1  A: 

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.

Phlibbo
I have tried printing out all notifications and respondsToSelector calls to the UIViewController and nothing is called when I click on the tabbar after the tab is already selected. So you're right, it does seem to be different behavior depending on if it is a UINavigationController or not
Ryan Bavetta
+2  A: 

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.

gcamp
thanks, optimally I'd want a solution that I could use on another tab in the future as well - but I think I can only set one delegate to the UITabBarController
Ryan Bavetta
You can set the delegate to your appDelegate object. And then send NSNotification or direct messages as you wish.
gcamp