刚刚创建了一个新项目,我有4个视图控制器,我将其添加到UINavigationController中,如下所示:

WatchViewController *first = [[WatchViewController alloc] init];
BetViewController *second = [[BetViewController alloc] init];
Settings *third = [[Settings alloc] init];
Account *forth = [[Account alloc] init];

UINavigationController *navFirst = [[UINavigationController alloc]initWithRootViewController:first];
UINavigationController *navSecond = [[UINavigationController alloc]initWithRootViewController:second];
UINavigationController *navThird = [[UINavigationController alloc]initWithRootViewController:third];
UINavigationController *navForth = [[UINavigationController alloc]initWithRootViewController:forth];


将它们加载到数组中:

NSArray *viewArray = [[NSArray alloc] initWithObjects:navFirst, navSecond, navThird, navForth, nil];


加载选项卡栏和窗口:

self.tabController = [[UITabBarController alloc] init];
[self.tabController setViewControllers:viewArray animated:YES];

[self.window setRootViewController:self.tabController];

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];


所有视图只是标准视图。当我尝试运行该应用程序时,它会响应:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'


我不知道我错过了什么。有什么帮助吗?

最佳答案

不要创建4个导航控制器。导航所需的控制器应该通过viewControllers方法分配给UINavigationController中的setViewControllers:animated:属性。

您应该创建1个NavigationController并添加4个UIViewControllers的数组。

此处提供了一个非常好的示例:example并且不要忘了在这里查看UINavigationClass

10-06 06:45