2012-05-03 52 views
3

在创建我的第一个iPhone应用程序的斗争中,我注意到苹果的例子有一个tabbar或导航栏,但从来都没有。带有tabbar和导航控制器的iOS应用程序

可以做到这一点吗?

所以我在这里有3个按钮的标签栏,我现在怎样才能将一个导航控制器添加到我的整个应用程序中?

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

UIViewController *activityViewController = [[[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil] autorelease]; 
UIViewController *agendaViewController = [[[AgendaViewController alloc] initWithNibName:@"AgendaViewController" bundle:nil] autorelease]; 
UIViewController *settingsViewController = [[[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil] autorelease]; 

self.tabBarController = [[[UITabBarController alloc] init] autorelease]; 
self.tabBarController.viewControllers = [NSArray arrayWithObjects:activityViewController, agendaViewController, settingsViewController, nil]; 
self.window.rootViewController = self.tabBarController; 
[self.window makeKeyAndVisible]; 

编辑:

我真的不继,所以在我的appdelegate我创建了一个navigationController和使用,作为RootViewController的。

然后,我创建了一个tabBarController并添加到了我的窗前

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 

UINavigationController *mainViewController = [[UINavigationController alloc] init]; 
self.mainViewController = [[UINavigationController alloc] initWithRootViewController:mainViewController]; 
self.window.rootViewController = self.mainViewController; 
[self.window makeKeyAndVisible]; 

self.window.rootViewController.title = @"test"; 

MainViewController *tabBarController = [[MainViewController alloc] init]; 
[self.window addSubview:tabBarController.view]; 

但每当我跑,我得到错误”

推导航控制器,不支持

我还缺少什么吗?

回答

0

你可以创建导航控制器,然后创建一个tabBarController和使用

//incase you have 2 navigation controllers 
tabBarController.viewControllers=[NSArray arrayWithObjects:navigationController1, navigationController2, nil ]; 
+0

他希望整个应用程序的navigationController。所以它必须是根 – Fab1n

+0

,如果他初始化标签栏控制器,如我在AppDelegate.m中所述,然后self.window.rootController = tabBarController;它会工作。 – Anila

0

嘿,这在概念上很容易的导航控制器添加到它:

你RootViewController的必须是你的navigationController,因为它是推动你的看法来来回回。

YourRootController *cont = [[YourRootController alloc] init]; 

UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:cont]; 

YourViewControllerUITabBarController

就是这样一个子类。

相关问题