简体   繁体   中英

Navigation among tab bar items Programmatically

I have a Tab bar Controller containing 5 Tabs and each tab bar item has individual navigation controller like : 在此处输入图片说明在此处输入图片说明

Now , If i am currently in viewctrller3 of navigationctrller3 in Tab3 and i want to navigate to viewctrller2 of navigationctrller1 in Tab1 programmatically . Also I've selected the particular Tab1 programmatically But im unable to reach viewctrller2 of navigationctrller1 . Is this feasible ?, If yes , then please elaborate.. Please guys join hands..!

If you are currently in viewctrller3, I think you should be able to do this:

    UITabBarController *tabCont = (UITabBarController *)[self.navigationController parentViewController];
    [tabCont setSelectedIndex:0];
    UINavigationController *nav1 = tabCont.selectedViewController;
    NSarray *newControllers = [NSArray arrayWithObjects:[nav1.viewControllers objectAtIndex:2],[nav1.viewControllers objectAtIndex:0],[nav1.viewControllers objectAtIndex:1], nil];
    [nav1 setViewcontrollers:newControllers animated:NO];

That last line should rearrange the navigation controller's viewControllers so that viewctrller2 is now the top view controller.

One way to achieve this is

  1. before setting the SelectedIndex of a tabbar , you maintain one reference variable in appDelegate and assign to it the name of the class which is required to be navigated after tab selection.
  2. In the delegate of tabbar controller :
    • (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController; check whether the visible controller is kind of class same as the class name that is set in appdelegate reference variable? If yes then nothing to do , if not then check if that controller is in the navigation stack or not? If its there in stack then pop/push to that index controller else create that controller and push to it.

I dont think that there will be any issue in this. suppose your nav1 is 1st navigationController.

[self.nav1 pushViewController:view2 animated:YES];

its will have to work for you.

From ViewController3 execute..

   UINavigationController *navigationController1 =       [appDelegate.tabBarController.viewControllers objectAtIndex:2]; // fetch the navigationController

            [navigationController1 popToViewController:[navigationController1.viewControllers objectAtIndex:1] animated:NO];

            [appDelegate .tabBarController setSelectedIndex:0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM