Search code examples
iphoneuinavigationcontrollertabbarcontroller

iphone app - uitabbarcontroller to always select first view in a uinavigationcontroller?


I am building an iphone application.

I have a tabbarcontroller that has 2 tab items. Each tabitem links to a different navigationcontroller. Each navigationcontroller links to a hierarchy of tableviewcontrollers. When a user clicks on tab 1, then clicks on an item in the table, then clicks tab 2, and then clicks on tab1, the application shows the table that he was just looking at before he clicked on tab2.

How do i get the app to show the first table of tab 1 every time he clicks on tab 1 instead of showing the most recent table he was looking at before leaving tab1?

I would prefer a programmatic solution as opposed to using xcode storyboard. But if none exists, then storyboard solution is fine too.


Solution

  • in my appdelegate.h file, I changed the line

    @interface wscAppDelegate : UIResponder <UIApplicationDelegate>
    

    to

    @interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
    

    Then in my CustomTabBarController in the viewDidLoad function i added these lines:

    wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
    self.delegate = appDelegate;
    

    Then in appdelegate.m file, I added this function

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
        for(int c=0; c<[tabBarController.viewControllers count]; c++)
        {
            UINavigationController * navcontroller = [tabBarController.viewControllers objectAtIndex:c];
    
            [navcontroller popToRootViewControllerAnimated:YES];
        }    
    
        return YES;
    
    }