如何以编程方式添加导航控制器?

时间:2014-02-06 17:31:05

标签: ios objective-c uinavigationcontroller

我使用下面的代码,但未加载:

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];

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:2)

将此代码添加到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];

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

YOUR_STORYBOARD_NAME是.storyboard文件的名称。例如,如果您的.storyboard文件被调用Main,请填写Main.storyboard

YOUR_VIEWCONTROLLER_ID是您的veiwController的ID。您可以在Identity inspector中进行修改。(见照片)

希望这会有所帮助:)

相关问题