ios 如何以编程方式添加导航控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21609921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add navigation controller programmatically?
提问by user3267017
I use code below, but it is not loaded:
我使用下面的代码,但没有加载:
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];
回答by freelancer
try as below
尝试如下
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];
回答by FireMango
Add this code to your AppDelegate.min the didFinishLaunchingWithOptionsfunction:
将此代码添加到您AppDelegate.m的didFinishLaunchingWithOptions函数中:
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];
yourViewControllerClassNameis the .h and .m file name that is linked to your viewController.
yourViewControllerClassName是链接到您的 viewController 的 .h 和 .m 文件名。
YOUR_STORYBOARD_NAMEis the name of your .storyboard file. For example, fill in Mainif your .storyboard file is called Main.storyboard.
YOUR_STORYBOARD_NAME是 .storyboard 文件的名称。例如,Main如果您的 .storyboard 文件名为,请填写Main.storyboard。
YOUR_VIEWCONTROLLER_IDis the ID for your veiwController. You can edit it in the Identity inspector.(See photo)
YOUR_VIEWCONTROLLER_ID是您的 veiwController 的 ID。您可以在Identity inspector.(见照片)中对其进行编辑


Hope this helps:)
希望这可以帮助:)

