在XCode 4.2中,当我选择“新项目”并选择“单视图应用程序”时,现在我想添加一个导航控制器。在Xcode 4.2中,我该怎么做呢?(不使用storyboard)
除非您将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;
- (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 苹果文档和他们的示例项目来进一步学习如何利用 UINavigationController。您可以从同一文档页面下载示例项目,这些示例项目将帮助您掌握各种使用 UINavigationController 的方法。
UINavigationController
类,并将其附加到你的delegate
类中,也就是在你的application delegate
类中定义一个IBOutLet UINavigationController
类,并在你的delegate类中定义它。在你的Interface Builder
中,将IBOutLet
连接到delegate类。