Tab bar, reload every time tab is pressed

16,630

Solution 1

Put the code you want to reload, in the view will appear or in view did appear of all the view.

All the best.

Solution 2

Example:

   // AppDelegate ( and <UITabBarControllerDelegate> )
   // Creation tabbar and controllers               
    UIViewController* myController1 = [[UIViewController alloc] init] autorelease];
    UINavigationController* nav1 = [[[UINavigationController alloc] initWithRootViewController:myController1] autorelease];

    UIViewController* myController2 = [[UIViewController alloc] init] autorelease];
    UINavigationController* nav2 = [[[UINavigationController alloc] initWithRootViewController:myController2] autorelease];

    NSArray *array = [NSArray arrayWithObjects: myController1, myController2, nil];

    UITabBarController* tab = [[[UITabBarController alloc] init] autorelease];
    tab.viewControllers = array;
    tab.delegate = self; // <-- don't forget set delegate for TabBarController


    // TabBarController Delegate methods   
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
    {
            // Reload selected VC's view
        [viewController.view setNeedsDisplay];
    }

Solution 3

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

    [self.tabBarController setDelegate:self]

    //other codes
    }

// UITabBarControllerDelegate method.
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController respondsToSelector:@selector(reloadDataTemp)]) {
        [(YourViewController *)viewController reloadData];
    }
}
Share:
16,630
Hiren
Author by

Hiren

Updated on June 27, 2022

Comments

  • Hiren
    Hiren almost 2 years

    I am creating an app in which I have five tabs. I need to reload each controller every time when tab is pressed.

  • Hiren
    Hiren almost 13 years
    thnx for reply.... but i need to make like whenever tab pressed it reload whole page...
  • Colin
    Colin over 8 years
    My code already was in viewDidAppear() and it doesn't refresh when the same tab bar button was pressed. The answer seems incomplete.