gpt4 book ai didi

ios - 如何保持标签栏顺序一致但更改默认 View Controller ?

转载 作者:行者123 更新时间:2023-11-29 10:51:28 25 4
gpt4 key购买 nike

我有几个 View Controller 连接到我的 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];

最佳答案

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

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 时,您应该能够将 View Controller 推送和弹出到导航堆栈中,而无需重建任何内容。

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

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];

关于ios - 如何保持标签栏顺序一致但更改默认 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20249014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com