Question

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.

Was it helpful?

Solution 3

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;

}

OTHER TIPS

Call popToRootViewControllerAnimated: on the NavigationController when the TabBarController changes tab that is being displayed.

Try this basic sample to create a UItabBar and UInavigationController for each UItabBarItem from scratch :

in your header file (appdelegate.h) , add this delegate :

@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>

in the function called "didFinishLaunchingWithOptions" , add this part of code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UINavigationController *navController=[[UINavigationController alloc] init];
m_ViewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil]; 
[navController pushViewController:m_ViewController1 animated:NO];

UINavigationController *navController2=[[UINavigationController alloc] init];
m_ViewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
[navController pushViewController:m_ViewController2 animated:NO];

UITabBarController *mtabBarController = [[UITabBarController alloc] init];
mtabBarController.view.frame = CGRectMake(0, 0, 320, 460);

// Set each tab to show an appropriate view controller
[mtabBarController setViewControllers:  [NSArray arrayWithObjects:navController1,navController1,navController2, nil]];

self.window.rootViewController = mtabBarController;    
mtabBarController.delegate = self;
[self.window makeKeyAndVisible];

return YES;
}

Then in this function , don't forget to add the popToRootViewControllerAnimated function :

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    [m_ViewController1.navigationController popToRootViewControllerAnimated:YES];
    [m_ViewController2.navigationController popToRootViewControllerAnimated:YES];
    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top