Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tab bar, reload every time tab is pressed

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

like image 298
Hiren Avatar asked May 10 '11 07:05

Hiren


3 Answers

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

All the best.

like image 124
Warrior Avatar answered Nov 04 '22 04:11

Warrior


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];
    }
like image 3
Alex Nazarov Avatar answered Nov 04 '22 04:11

Alex Nazarov


 - (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];
    }
}
like image 2
KarenAnne Avatar answered Nov 04 '22 04:11

KarenAnne