How to add navigation controller programmatically?

假装没事ソ 提交于 2019-12-18 17:29:39

问题


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

回答1:


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



回答2:


Add this code to your AppDelegate.m in the didFinishLaunchingWithOptions function:

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 is the .h and .m file name that is linked to your viewController.

YOUR_STORYBOARD_NAME is the name of your .storyboard file. For example, fill in Main if your .storyboard file is called Main.storyboard.

YOUR_VIEWCONTROLLER_ID is the ID for your veiwController. You can edit it in the Identity inspector.(See photo)

Hope this helps:)



来源:https://stackoverflow.com/questions/21609921/how-to-add-navigation-controller-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!