2012-05-03 49 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

就是這樣一個子類。

相關問題