iPhone SDK - Add a UINavigationController Programm

2020-06-16 08:47发布

I have an iPhone project which currently does not have a UINavigationController in it. Instead I swap views by handling the view hierarchy myself. However - as you're probably aware - this is bad practice, so I am trying to figure out how to implement a UINavigationController into my current app. I can obviously start my project over and use an Xcode template, but I really don't want to do this.

There must be a way to do this programmatically. But I can't figure out what code needs to go in the AppDelegate. Does anyone have any experience with this? I am at a loss at the moment.

Cheers, Brett

6条回答
狗以群分
2楼-- · 2020-06-16 09:06

example of creating and releasing a navigation controller:

UINavigationController *navCon = [[UINavigationController alloc] init];
[navCon pushViewController:yourViewController animated:NO];
[navCon release];
查看更多
【Aperson】
3楼-- · 2020-06-16 09:16

I resolved the same problem using this.

 - (void)applicationDidFinishLaunching:(UIApplication *)application 
 {
      navController=[[UINavigationController alloc] init];
      MyViewController *firstController=[[MyViewController alloc] init];
      self.window.rootViewController = firstController;
      [self.window addSubview navController.view];

 } 
查看更多
看我几分像从前
4楼-- · 2020-06-16 09:19

in Xcode 4 in the interface editor, open the initial view controller, and then select 'editor > embed in >navigation controller'

查看更多
Deceive 欺骗
5楼-- · 2020-06-16 09:20

![I added navigation controller into my tab bar project and it is working perfectly

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];

UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];

UIViewController *viewController3 = [[[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil] autorelease];

self.tabBarController = [[[UITabBarController alloc] init] autorelease];


self.tabBarController.viewControllers = @[viewController1, viewController2,viewController3];


self.navigationController= [[UINavigationController alloc]initWithRootViewController:self.tabBarController];

// self.window.rootViewController = self.tabBarController;

self.window.rootViewController = self.navigationController;


[self.window makeKeyAndVisible];


return YES;
查看更多
Explosion°爆炸
6楼-- · 2020-06-16 09:22

Yeap.

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
   navController=[[UINavigationController alloc] init];
   MyViewController *firstController=[[MyViewController alloc] init];
   [navController pushViewController:firstController animated:NO];
   [window addSubview: navController.view];
} 
- (void) dealloc
{
   ...  
   [navController release];
   ...
}

It's a quite common question, have a look at this too.... Programmatically add UINavigationController in UIViewController

查看更多
够拽才男人
7楼-- · 2020-06-16 09:26
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

// application.applicationIconBadgeNumber = 0;
navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
查看更多
登录 后发表回答