2014-02-06 155 views
4

我使用代碼波紋管,但沒有加載:如何以編程方式添加導航控制器?

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
self.mapViewController = [storyboard instantiateViewControllerWithIdentifier:@"MapViewController"]; 
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self]; 

self.navigationBar = [[UINavigationBar alloc]init]; 
[self.view addSubview:self.navigationBar]; 

[self.navigationController.navigationController pushViewController:self.mapViewController animated:YES]; 
+0

'self.navigationController.navigationController'永遠是'nil'。導航控制器永遠不會在另一個導航控制器中。 – rmaddy

+0

在視圖控制器中創建並分配像這樣的導航控制器也沒有任何意義。通常情況下,您將創建更高級別的導航控制器並使用根視圖控制器進行設置。然後,當視圖控制器想要推動另一個視圖控制器(如這裏)時,只需將新視圖控制器推到'self.navigationController'上。 – rmaddy

+0

沒有它,它不顯示.. – user3267017

回答

9

嘗試如下

UIViewController *bbp=[[UIViewController alloc]initWithNibName:@"UIViewController" bundle:nil]; 
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:bbp]; 
// [self.navigationController presentModalViewController:passcodeNavigationController animated:YES]; 
    [self.navigationController pushViewController:passcodeNavigationController animated:YES]; 
    [passcodeNavigationController release]; 
+0

謝謝,我看了很多問題,但只有這個答案幫助我:)工作在iOS 9。 –

2

didFinishLaunchingWithOptions功能將此代碼添加到您的AppDelegate.m

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_NAME" bundle:nil]; 
yourViewControllerClassName *vc = [sb instantiateViewControllerWithIdentifier:@"YOUR_VIEWCONTROLLER_ID"]; 
self.navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; 
self.window.rootViewController = self.navigationController; 
[self.window makeKeyAndVisible]; 

yourViewControllerClassName是鏈接到您的viewController的.h和.m文件名。

YOUR_STORYBOARD_NAME是您的.storyboard文件的名稱。例如,如果您的.storyboard文件被稱爲Main.storyboard,請填寫Main

YOUR_VIEWCONTROLLER_ID是您的veiwController的ID。您可以在Identity inspector編輯。(見照片)

希望這有助於:)

相關問題