Go to rootView when clicking tabBar

I used a view-based application and in that I programmatically generate a TabBar. The problem is that:

I have an Iphone application in which I have 2 tabitems using tabbarcontroller. Within the tabbarcontroller, each view controller is a navigation controller. When selecting the second tab, I have a view controller. When choosing a button on this, I click another kind of controller to the self.navigation controller. And in this view manager, I click and I want it like that. But the problem is that I again select the tabitem that the pushedviewcotrooller is shown there. But I need this rootview there again when I select 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
source share
5 answers

You may be looking for:

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

In fast mode, you can do this in your UITabBarController class:

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

I believe that you will need to use these two methods:

UINavigationController : - popToRootViewControllerAnimated :

 UITabBarControllerDelegate: tabBarController:didSelectViewController: 

The approach that I use in my own program is to show only the tab bar when the root view controller is on the screen.

0
source

Add UITabBarControllerDelegate to your AppDelegate method and in didFinishLaunchingWithOptions , set the delegate as UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self; UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; tabBarController.delegate = self; . Then the delegate method will be called - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController when selecting the scoreboard.

0
source

In Swift 3.1

Add a UITabBarControllerDelegate to your TabBar class:

 class YourClass: UITabBarController, UITabBarControllerDelegate { 

After:

 override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) { let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController yourView .popToRootViewControllerAnimated(false) } 
-1
source

All Articles