Search code examples
iphoneobjective-ciosuitabbarcontrolleruitabbaritem

suffering with UITabbaritems


For some days now I have been suffering with UITabBarItems.

I have done my application in programatical way, not using Interface Builder. I have both a UINavigationBar and a UITabbarController.

From home page when I have proceed with navigation (I mean when I move to next page) at that time when i have clicked next tab item (Contact), and again when I have clicked the home button, it is not moving to home page, it is remaining with the previous page where I left that one, it is not redirecting to home page at all,

I have placed the UITabbarController-code in my application delegate:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.tabBarController = [[UITabBarController alloc] init];
viewController *vc = [[viewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];
[navController.tabBarItem setTitle:@"Home"];
[navController.tabBarItem setImage:[UIImage imageNamed:@"home.png"]];

viewController1 *vc1 = [[viewController1 alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:vc1];
[navController1.tabBarItem setTitle:@"Contact"];
[navController1.tabBarItem setImage:[UIImage imageNamed:@"contact.png"]];

viewController2 *vc2 = [[MapViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:vc2];
[navController2.tabBarItem setTitle:@"Info"];
[navController2.tabBarItem setImage:[UIImage imageNamed:@"info.png"]];

NSArray *viewControllers = [NSArray arrayWithObjects:navController, navController1,navController2,  nil];
 _tabBarController.delegate = self;
[_tabBarController setViewControllers:viewControllers];

[self.window setRootViewController:_tabBarController];
[self.window makeKeyAndVisible];
 return YES;

I didn't used these methods:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 

Please give me suggestions, or something that may be useful to me.


Solution

  • Default behaviour of UITabbarController is, that if you tap a tab and this tab contains a navigationcontroller, you get the page which is on top of the navigationcontrollers viewcontroller-stack.

    To avoid this you have to popToRootViewController when you tap on the tab.

    If you implement

    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
     {
        [viewController.navigationController popToRootViewControllerAnimated:NO];
     }
    

    it should do the trick.