Ios 如何在更改默认视图控制器的同时保持选项卡栏顺序的一致性?

Ios 如何在更改默认视图控制器的同时保持选项卡栏顺序的一致性?,ios,objective-c,uiviewcontroller,uitabbarcontroller,Ios,Objective C,Uiviewcontroller,Uitabbarcontroller,我有几个视图控制器连接到我的tabbarcontroller。每一个都被相应地命名为FirstVC,SecondVC等。当我在ThirdVC上,按下一个按钮时,我想让它显示另一个VC,我们称之为ThirdChildVC,然后在ThirdChildVC上有一个按钮,返回到ThirdVC。我能做到这一点的唯一方法是包含以下代码: [[UIApplication sharedApplication]委托]性能选择器:@selector(setupRootViewController1)] 调用另一个方

我有几个视图控制器连接到我的tabbarcontroller。每一个都被相应地命名为FirstVC,SecondVC等。当我在ThirdVC上,按下一个按钮时,我想让它显示另一个VC,我们称之为ThirdChildVC,然后在ThirdChildVC上有一个按钮,返回到ThirdVC。我能做到这一点的唯一方法是包含以下代码:

[[UIApplication sharedApplication]委托]性能选择器:@selector(setupRootViewController1)]

调用另一个方法,我们称之为setupRVC1,它与setupRVC(见下文)相同,只是VC的顺序显示在选项卡中。但是,我更希望能够保持tabbaritems的顺序,以便它始终按顺序显示FirstVC、SecondVC等,但能够在按下ThirdChildVC上的按钮时将ThirdVC显示为默认VC

- (void)setupRVC
{
    UIViewController *firstVC = [[FirstVC alloc]initWithNibName:@"FirstVC" bundle:nil];
    UIViewController *secondVC = [[SecondVC alloc]initWithNibName:@"SecondVC" bundle:nil];
    UIViewController *thirdVC = [[ThirdVC alloc]initWithNibName:@"ThirdVC" bundle:nil];
    UIViewController *fourthVC = [[FourthVC alloc]initWithNibName:@"FourthVC" bundle:nil];
    UIViewController *fifthVC = [[FifthVC alloc]initWithNibName:@"FifthVC" bundle:nil];
    self.tabBarController = [[UITabBarController alloc]init];
    self.tabBarController.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

- (void)setupRVC1
{
    UIViewController *firstVC = [[FirstVC alloc]initWithNibName:@"FirstVC" bundle:nil];
    UIViewController *secondVC = [[SecondVC alloc]initWithNibName:@"SecondVC" bundle:nil];
    UIViewController *thirdVC = [[ThirdVC alloc]initWithNibName:@"ThirdVC" bundle:nil];
    UIViewController *fourthVC = [[FourthVC alloc]initWithNibName:@"FourthVC" bundle:nil];
    UIViewController *fifthVC = [[FifthVC alloc]initWithNibName:@"FifthVC" bundle:nil];
    self.tabBarController = [[UITabBarController alloc]init];
    self.tabBarController.viewControllers = @[ThirdVC, FirstVC, SecondVC, ThirdVC, FourthVC, FifthVC];
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

您需要做的是为每个视图控制器创建一个导航控制器,然后将所有导航控制器添加到选项卡栏控制器中。下面是我创建的一个应用程序的示例

NSMutableArray *tabBarItems = [@[] mutableCopy];
WorkingTableViewController *workingTableVC = [[WorkingTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *workingNavController = [[UINavigationController alloc] initWithRootViewController:workingTableVC];

ClosedTableViewController *closedTableVC = [[ClosedTableViewController alloc] init];
UINavigationController *closedNavController = [[UINavigationController alloc] initWithRootViewController:closedTableVC];

[tabBarItems addObject:workingNavController];
[tabBarItems addObject:closedNavController];

tabBarController.viewControllers = tabBarItems;
设置好后,每次将选项卡切换到不同的VC时,您应该能够将视图控制器推入并弹出到导航堆栈中,而无需重建任何内容

编辑:试试这个,让我知道它是否有效

UIViewController *firstVC = [[FirstVC alloc]initWithNibName:@"FirstVC" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:firstVC];

UIViewController *secondVC = [[SecondVC alloc]initWithNibName:@"SecondVC" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];

UIViewController *thirdVC = [[ThirdVC alloc]initWithNibName:@"ThirdVC" bundle:nil];
UINavigationController *thirdNavController = [[UINavigationController alloc] initWithRootViewController:thirdVC];

UIViewController *fourthVC = [[FourthVC alloc]initWithNibName:@"FourthVC" bundle:nil];
UINavigationController *fourthNavController = [[UINavigationController alloc] initWithRootViewController:fourthVC];

UIViewController *fifthVC = [[FifthVC alloc]initWithNibName:@"FifthVC" bundle:nil];
UINavigationController *fifthNavController = [[UINavigationController alloc] initWithRootViewController:fifthVC];

self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.viewControllers = @[firstNavController, secondNavController, thirdNavController, fourthNavController, fifthNavController];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

很好,但是什么是ClosedTableViewController?ClosedTableViewController只是UITableViewController的一个子类。正如我提到的,这些只是我项目中的课程。我将更新代码以使用您的类。