1

I have set up a delegate for my UITabBarController and have the following delegate method:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {    
        [self.navigationController popToRootViewControllerAnimated: YES];
        return NO;
}

The method fires and in the context, I'm one or two levels into table views. The root view controller gets displayed correctly, but the navigation bar doesn't get reset, and still has a "back" button from one or two levels into the table views.

(The above delegate method is a simplified form of what I'm trying to do in my app, but still exhibits the problem. In my app, I need the root view displayed when I return to the original tab, so am trying to pop to the root view before I leave).

popToRootViewControllerAnimated not working correctly suggests a time lag issue, but that doesn't appear to be what's going on in my case. I can wait as long as I want before doing the shouldSelectViewController and the nav bar "back" button still is messed up.

The delegate is:

@interface BasicPlaceItemComment : UIViewController<UIAlertViewDelegate, UITabBarControllerDelegate> {
    // data members ommitted
} 

This is the class from which I derive all of my table views.

Community
  • 1
  • 1
Chris Prince
  • 7,288
  • 2
  • 48
  • 66

2 Answers2

0

Are you sure the method is in the right context? Who conforms to the TabBarDelegate? If it's your AppDelegate, then self.navigationController might be nil. If you're using a TabBar and you have more than one navigation controller, then you might be calling popToRootViewController on the wrong controller. Try to insert this:

NSLog(@"%@", self.navigationController);

at the beginning of your method and make sure you get the address of the right navigation controller.

Posting some more code might be helpful.

Eli Ganem
  • 1,479
  • 1
  • 13
  • 26
0

The issue seems to be that I was subclassing UINavigationController to solve a different problem. When I remove this subclass, the issue goes away. That is, the "back" buttons are not messsed up anymore when I do popToRootViewController without subclassing UINavigationController. This, however, raises another problem. I was subclassing UINavigationController in order to solve the problem stated in UINavigationController and UINavigationBarDelegate.ShouldPopItem() with MonoTouch

With both my nav bar and my tab bar I want to be able to put up a message "Are you sure you want to Quit?" alert when the back button (or another tab) is pressed in the case when some data in the displayed view has changed (before navigating away from that view, and possibly canceling navigating away from that view).

So, while this a partial answer, my problem still stands. I still need a means to gain control when the user types the back button or another tab. So, it seems I need: 1) A means to have a different delegate than the UINavigationController for UINavigationBarDelegate method shouldPopItem, or 2) Some means to subclass UINavigationController but not get the back button messed up as is currently happening when I click on a tab and call popToRootViewController.

Further thoughts?

More on 11/3/12

It's not UINavigationController subclassing per se that was causing the issue. I was inadvertently returning NO from my delegate method

// This method returns true when the navigation bar should pop an item
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

when I did a popToRootViewController. I now have a method in my UINavigationController subclass:

- (void) popToRoot {
    regularPop = YES;
    [self popToRootViewControllerAnimated: YES];
}

that causes shouldPopItem to return YES, which I use instead of popToRootViewController. Most of my problems are solved!

Community
  • 1
  • 1
Chris Prince
  • 7,288
  • 2
  • 48
  • 66