Problem clicking on the root navigation controller on the tab switch

Trying to simulate / copy the built-in address book, in particular, when editing a contact or viewing existing contact information from the Phone application. When you switch to another tab, the edit state is reset, and the message “New Contact” or “Information” opens, so when you return to the “Contacts” tab, you will be returned to the root table view.

I have most of this work inside viewWillDisappear using setEditing: and popToViewController: however, I get weird behavior when the user goes from the Info view to the table view using the back button. Even if I appear in the root table view controller, it looks like it uses the default UITableViewController class and not my subclass (for example, standard selection behavior instead of my overrides to push the detailed view.)

Any clues? IPD

Here is some code to illustrate:

- (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // This is to clean up from the colored bar in detail view self.navigationController.navigationBar.tintColor = nil; // These are to match the behaviour of Contacts app [self setEditing:NO animated:NO]; // This is the tricky part: works when switching tabs, but not when back button was going to pop anyway!! [self.navigationController popToViewController:rootViewControllerForTab animated:NO]; } 
+4
source share
1 answer

The -viewWillDisappear method: this is not the best place to change the view controller stack for your navigation controller, because it works both when switching tabs and when displaying a view on top of it.

I played around a bit with this and found that the best place for this is the method - [UITabBarControllerDelegate tabBarController: didSelectViewController:]. So, first you need to set the object as a delegate for the tab bar (I used the application delegate). Bind the delegate property of your UITabBarController to an object that implements the UITabBarControllerDelegate protocol in code or in Interface Builder.

Then we implement the -tabBarController: didSelectViewController: method. The trick now is how to say when the address book tab is switched. I tracked the view controller for the tab in question using a property of type UINavigationController (the root view controller for the tab). After binding the tab1NavController property to the actual instance using Interface Builder, you can use it to compare with the viewController parameter to see which tab was selected only.

 @interface Pop2RootTabSwitchAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { UINavigationController *tab1NavController; } @property (nonatomic, retain) IBOutlet UINavigationController *tab1NavController; @end @implementation Pop2RootTabSwitchAppDelegate - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { NSLog(@"[%@ tabBarController:%@ didSelectViewController:%@]", [self class], tabBarController, viewController); if (viewController == tab1NavController) { NSLog(@"viewController == tab1NavController"); [tab1NavController popToRootViewControllerAnimated:NO]; } } 
+5
source

All Articles