5
votes

I used view based app & in that i programmatically generate TabBar. Problem is:

I have an Iphone application in which i have 2 tabitems with a tabbarcontroller.Inside the tabbarcontroller each viewcontroller is a navigation controller.when selecting the second tab i have a view controller.when selecting a button on that i am pushing another view controller to the self.navigation controller.and in that viewcontroller i am pushing and go like that.But the problem is when i am selecting the tabitem again that pushedviewcotrooller is shown there.but i need that rootview there again when i am selecting the tab

my code in AppDelegate.m is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

 UINavigationController *nc1;
    nc1 = [[UINavigationController alloc] init];

UIViewController *viewController1 = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];





    UINavigationController *nc2;
    nc2 = [[UINavigationController alloc] init];

 UIViewController *viewController2 = [[[secondview alloc] initWithNibName:@"secondview" bundle:nil] autorelease];
    nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];


    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
   self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil];
  self.window.rootViewController=self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}
4
can you please more describe your problem in more briefly so i can understand your problem.Nimit Parekh
when i tap on any tabbar item it only goes the viewcontroller on which i come by navigation. i want when i tap on tabbar it must go to rootview controller.piyush

4 Answers

12
votes

May be you are looking for this :

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{       
    int tabitem = tabBarController.selectedIndex;
    [[tabBarController.viewControllers objectAtIndex:tabitem] popToRootViewControllerAnimated:YES];
}
4
votes

In swift you can do it this way in your UITabBarController class:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
    rootView.popToRootViewControllerAnimated(false)
}
0
votes

I believe you will need to employ these two methods:

UINavigationController: -popToRootViewControllerAnimated:

UITabBarControllerDelegate: tabBarController:didSelectViewController:

The approach I am using in my own program is to only show the tab bar while a root view controller is on the screen.

0
votes

Add UITabBarControllerDelegate to your AppDelegate, and in didFinishLaunchingWithOptions method, set the delegate like UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self;. Then the delegate method - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController will be invoked when tabbar is selected.