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

时间:2013-11-27 17:21:10

标签: ios objective-c uiviewcontroller uitabbarcontroller

我有几个视图控制器连接到我的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 个答案:

答案 0 :(得分:2)

您需要做的是为View Controller创建一个导航控制器,然后将所有导航控制器添加到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];