如何保持标签栏顺序一致但更改默认视图控制器?

Posted

技术标签:

【中文标题】如何保持标签栏顺序一致但更改默认视图控制器?【英文标题】:how can I keep the tab bar order consistent but change the default view controller? 【发布时间】:2013-11-27 17:21:09 【问题描述】:

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

[[[UIApplication sharedApplication] delegate] performSelector:@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];

【问题讨论】:

【参考方案1】:

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

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 的一个子类。正如我所提到的,这些只是我项目中的类。我将更新代码以使用您的类。

以上是关于如何保持标签栏顺序一致但更改默认视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章

自定义标签栏问题

弹出窗口出现时如何阻止标签栏项目变灰

在 iPhone 中推送 UIViewController 时如何更改标签栏项目的选定索引

Swift 2:更改与选项卡栏控制器相关的视图上的导航标题

如何让自定义标签栏显示标签栏项目在 Xcode 中设置的选定图像?

根据标签栏索引更改视图控制器的属性?