2014-02-06 107 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编辑。(见照片)

希望这有助于:)

相关问题