views:

47

answers:

2

Hi All!

I have a UITabBarController with a set of tabs. Some of them, when selected, show you a table. The cells of the table, if touched, bring the user to a detail screen of the object in the cell itself using a "pushViewController".

When I click to a different tab, and I am in the detail page, I see the new tab page. And this is fine. When I click back to the previous tab I would like to have shown again the table and not the detail page...

Is this possible?

Thanks!

+1  A: 

It is possible. Code:

// Called when the user selected a tab. In iOS3+, called even when no change occurs.
- (void)tabBarController:(UITabBarController *)tabbarcontroller didSelectViewController:(UIViewController *)viewController
{
    UINavigationController *nav = (UINavigationController *)viewController;
    [nav popToRootViewControllerAnimated: NO];
}

Explanations:

  • this is in your AppDelegate.
  • Your AppDelegate is a UITabBarControllerDelegate.
  • You set your UITabBarController's delegate (in InterfaceBuilder) to the AppDelegate.
  • Your tabs must contain root controllers that are navigation controllers (must be, else your wouldn't be able to 'push' details).
jv42
Thanks for your suggestion, but I can't get it working.It looks like the method is never called... since I have added a log and it never appears...
0m4r
A: 

See the UITabBarControllerDelegate protocol to set up which controller will be notified when tabs are switched then just pop the detail controller using that.

BTW, you can also do most of this navigation controller setup in interface builder like this:

alt text

Nimrod
This I have and it works, the only probem is what the snippet of code you gave me. It looks like that method is not being called...
0m4r
It wasn't me that gave you the code snippet. Anyway, if I understand your question you want to detect when someone switches tabs. you can use the tab bar delegate to do that. Look up UITabBarController, UITabBar and look for the delegate property.
Nimrod
You got the point, I found I forgot to set the delegate controller.
0m4r
what I did is to add tabBarController.delegate = self; to application:didFinishLaunchingWithOptions: and now it works!Thanks a lot!
0m4r