0

刚刚创建了一个新项目,我添加了 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

4 回答 4

1

不要创建 4 个导航控制器。需要导航的控制器应分配给via方法中的viewControllers属性。UINavigationControllersetViewControllers:animated:

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

这里给出了一个很好的例子:example,别忘了看这里UINavigationClass

于 2013-07-01T12:13:42.077 回答
1

试试这个:

WatchViewController *first = [[WatchViewController alloc] initWithNibName:@"WatchViewController" bundle:Nil];
BetViewController *second = [[BetViewController alloc] initWithNibName:@"BetViewController" bundle:Nil];
Settings *third = [[Settings alloc] initWithNibName:@"Settings" bundle:Nil];
Account *forth = [[Account alloc] initWithNibName:@"Account" bundle:Nil];

/*Your View Navigation Stuff and your viewArray*/

self.tabController.viewControllers = viewArray;
于 2013-07-01T12:10:52.253 回答
1

你为什么不通过在 xib 中设置控制器来尝试UINavigationController内部UITabBarController。它对我有用。

于 2013-07-01T12:20:42.693 回答
0

抱歉,我烦人地继承了 UINavigationController 而不是 UIViewController。我没有接受它,因为如果我不使用导航控制器,应用程序会运行良好,添加导航控制器就会坏掉。:(

于 2013-07-01T13:09:18.767 回答