解决方法
除非您将UINavigationController添加到另一个用于不同导航方法的UIViewController,即UISplitViewController或UITabBarController,我建议将UINavigationController添加到AppDelegate中的应用程序窗口,然后添加包含您视图的UIViewController.
如果要将UINavigationController添加为主UIViewController,则可以通过AppDelegate中的以下方法以编程方式轻松执行此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
UINavigationController *navcon = [[UINavigationController alloc] init]; [navcon pushViewController:self.viewController animated:NO]; self.window.rootViewController = navcon;
现在,在你的AppDelegate.m中它应该如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease]; } else { self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease]; } UINavigationController *navcon = [[UINavigationController alloc] init]; [navcon pushViewController:self.viewController animated:NO]; self.window.rootViewController = navcon; [self.window makeKeyAndVisible]; return YES; }
您可以通过查看UINavigationController Apple Documentation及其示例项目来进一步了解如何利用UINavigationController,您可以从同一文档页面下载这些项目.示例项目将帮助您掌握使用UINavigationController的各种方法.