iOS:Xcode 4.2和导航控制器

前端之家收集整理的这篇文章主要介绍了iOS:Xcode 4.2和导航控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在XCode 4.2中,当我选择“新项目”并选择“单一视图应用程序”但现在我想添加一个导航控制器.我在 Xcode 4.2中可以做些什么呢? (没有故事板)

解决方法

除非您将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的各种方法.

猜你在找的Xcode相关文章